简体   繁体   English

用C ++读写文件

[英]Reading and writing to a file in c++

I am trying to write a triple vector to a file and then be able to read back into the data structure afterward. 我正在尝试将三元向量写入文件,然后能够再读回数据结构。 When I try to read the file back after its been saved the first fifty values come out correct but the rest of the values are garbage. 当我尝试在保存文件后回读该文件时,前50个值正确无误,但其余的值都是垃圾。 I'd be really appreciative if someone could help me out here. 如果有人可以在这里帮助我,我将不胜感激。 Thanks a lot! 非常感谢!

File declaration: 文件声明:

    fstream memory_file("C:\\Users\\Amichai\\Pictures\\output.txt", ios::in | ios::out);    

Save function: 保存功能:

void save_training_data(fstream &memory_file, vector<vector<vector<long double> > > &training_data)
 {
   int sizeI = training_data.size();
   memory_file.write((const char *)&sizeI, sizeof(int));
   for (int i=0; i < sizeI; i++)
   {
       int sizeJ = training_data[i].size();
       memory_file.write((const char *)&sizeJ, sizeof(int));
       for (int j=0; j < sizeJ; j++) 
       {
           int sizeK = training_data[i][j].size();
           memory_file.write((const char *)&sizeK, sizeof(int));
           for (int k = 0; k < sizeK; k++)
           {
               int temp;
               temp = (int)training_data[i][j][k];
               memory_file.write((const char *)&temp, sizeof(int));
           }
       }
   } 
 }

Read function: 读取功能:

void upload_memory(fstream &memory_file, vector<vector<vector<long double> > > &training_data)
{
     memory_file.seekg(ios::beg);
     int temp=0;
     int sizeK, sizeJ, sizeI; 
     memory_file.read((char*)&sizeI, sizeof(int));
     training_data.resize(sizeI);
     for (int i=0; i < sizeI; i++)
     {
           memory_file.read((char*)&sizeJ, sizeof(int));
           training_data[i].resize(sizeJ);           
           for (int j=0; j < sizeJ; j++)
           {
               memory_file.read((char*)&sizeK, sizeof(int));
               training_data[i][j].resize(sizeK);
               for (int k = 0; k < sizeK; k++)
               {
                    memory_file.read((char*)&temp, sizeof(int));
                    training_data[i][j][k]=temp;
               }
           }
     } 
}

由于您正在编写二进制数据(并且显然在Windows下工作),因此在打开fstream时,确实需要指定ios :: binary。

The problem is that you're writing the numerical values in binary form to a file interpreted as text by the stream processor. 问题是您正在将二进制形式的数值写入流处理器解释为文本的文件中。 Either use a binary file (using ios::binary) or convert the numbers to strings before writing to file. 在写入文件之前,请使用二进制文件(使用ios :: binary)或将数字转换为字符串。

Check out the Boost.Serialization library at www.booost.org . www.booost.org上检查Boost.Serialization库。 It knows how to read and write STL collections to/from files. 它知道如何读写文件中的STL集合。 I don't know if it can handle nested containers, though. 不过,我不知道它是否可以处理嵌套容器。

You may also want to use Boost.Multiarray for your 3-dimensional data. 您可能还想对3维数据使用Boost.Multiarray。 If you're going to do matrix math on your data, then you might want to use Boost.uBlas. 如果要对数据进行矩阵数学运算,则可能要使用Boost.uBlas。

As the other answers suggest using "ios::in | ios::out | ios::binary" instead of "ios::in | ios::out" which is correct, however I remember reading that the C++ stream specification while having the binary option was not designed for binary files at all. 其他答案建议使用“ ios :: in | ios :: out | ios :: binary”代替“ ios :: in | ios :: out”,这是正确的,但是我记得在阅读C ++流规范的同时二进制选项根本不是为二进制文件设计的。 If using "ios::binary" doesn't help you would need to use the C function fopen(), fread(), fwrite(), and fclose() of stdio.h instead or, as another user suggests, the Boost::Serilization library. 如果使用“ ios :: binary”无济于事,则您需要使用stdio.h的C函数fopen(),fread(),fwrite()和fclose(),或者按照另一个用户的建议使用Boost ::序列化库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM