相关疑难解决方法(0)

用C++编写二进制文件的速度非常快

我正在尝试将大量数据写入我的SSD(固态硬盘).大量的我的意思是80GB.

我浏览网页寻求解决方案,但我想出的最好的是:

#include <fstream>
const unsigned long long size = 64ULL*1024ULL*1024ULL;
unsigned long long a[size];
int main()
{
    std::fstream myfile;
    myfile = std::fstream("file.binary", std::ios::out | std::ios::binary);
    //Here would be some error handling
    for(int i = 0; i < 32; ++i){
        //Some calculations to fill a[]
        myfile.write((char*)&a,size*sizeof(unsigned long long));
    }
    myfile.close();
}
Run Code Online (Sandbox Code Playgroud)

使用Visual Studio 2010进行编译并完全优化并在Windows7下运行,此程序最大可达20MB/s.让我感到困扰的是,Windows可以将文件从其他SSD复制到此SSD,速度介于150MB/s和200MB/s之间.所以至少快7倍.这就是为什么我认为我应该能够更快.

我有什么想法可以加快我的写作速度?

c++ io optimization performance file-io

221
推荐指数
8
解决办法
17万
查看次数

读写C++向量到文件

对于某些图形工作,我需要尽快读取大量数据,理想情况下是直接读取和写入数据结构到磁盘.基本上我有各种各样的文件格式的3D模型,加载时间太长,所以我想把它们以"准备好"的格式写出来作为缓存,在后续的程序运行中加载速度要快得多.

这样做是否安全?我担心的是直接读入载体的数据?我已经删除了错误检查,硬编码4作为int的大小等等,所以我可以给出一个简短的工作示例,我知道这是错误的代码,我的问题是如果在c ++中读取整个数组是安全的结构直接进入这样的矢量?我相信它是这样的,但是当你开始进入低级并直接处理像这样的原始内存时,c ++有很多陷阱和未定义的行为.

我意识到数字格式和大小可能会在平台和编译器之间发生变化,但这只会由同一个编译器程序读取和写入,以缓存稍后运行同一程序时可能需要的数据.

#include <fstream>
#include <vector>

using namespace std;

struct Vertex
{
    float x, y, z;
};

typedef vector<Vertex> VertexList;

int main()
{
    // Create a list for testing
    VertexList list;
    Vertex v1 = {1.0f, 2.0f,   3.0f}; list.push_back(v1);
    Vertex v2 = {2.0f, 100.0f, 3.0f}; list.push_back(v2);
    Vertex v3 = {3.0f, 200.0f, 3.0f}; list.push_back(v3);
    Vertex v4 = {4.0f, 300.0f, 3.0f}; list.push_back(v4);

    // Write out a list to a disk file
    ofstream os ("data.dat", ios::binary);

    int size1 = list.size();
    os.write((const …
Run Code Online (Sandbox Code Playgroud)

c++ file-io vector

17
推荐指数
3
解决办法
4万
查看次数

写入二进制文件

#include <iostream>
#include <fstream>

using namespace std;

class info {

private:
    char name[15];
    char surname[15];
    int age;
public:
    void input(){
        cout<<"Your name:"<<endl;
            cin.getline(name,15);
        cout<<"Your surname:"<<endl;
        cin.getline(surname,15);
        cout<<"Your age:"<<endl;
        cin>>age;
        to_file(name,surname,age);
    }

    void to_file(char name[15], char surname[15], int age){
        fstream File ("example.bin", ios::out  | ios::binary | ios::app);
    // I doesn't know how to fill all variables(name,surname,age) in 1 variable (memblock) 
        //example File.write ( memory_block, size ); 

File.close();
    }

};

int main(){

info ob;
ob.input();

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何将1个以上的变量写入文件,请帮助,我包含了一个例子;)也许有更好的方法来写一个文件,请帮我这个,这对我来说很难解决.

c++ binary fstream

8
推荐指数
2
解决办法
3万
查看次数

如何从C中的二进制文件中读取浮点数?

一切我通过谷歌发现是垃圾...请注意,我想要的答案 Ç,但是如果你用C++解决方案补充你的答案,以及那么你获得积分!

我只是想能够从二进制文件中读取一些浮点数到一个数组中

编辑:是的我知道Endian-ness ...而且我不在乎它是如何存储的.

c c++ file-io

6
推荐指数
2
解决办法
3万
查看次数

将int写入C中的二进制文件时出现问题

我需要使用C的I/O函数将数据写入二进制文件.以下代码导致运行时异常:


#include "stdio.h"

int main(int argc,char* argv[]) {
    FILE *fp = fopen("path_to_file.bin","wb");
    if(fp == NULL) {
        printf("error creating file");
        return -1;
    }
    int val = 4;
    fwrite((const void*)val,sizeof(int),1,fp);
    fclose(fp);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

代码在fwrite处死掉.你能发现我做错了什么吗?显然,我正试图访问0x0000004或类似的数据.

谢谢 !

c binary int file

3
推荐指数
3
解决办法
3万
查看次数

标签 统计

c++ ×4

file-io ×3

binary ×2

c ×2

file ×1

fstream ×1

int ×1

io ×1

optimization ×1

performance ×1

vector ×1