Amichai
Amichai

Reputation: 13

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. 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;
               }
           }
     } 
}

Upvotes: 1

Views: 560

Answers (4)

jlamp
jlamp

Reputation: 1

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. 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.

Upvotes: 0

Emile Cormier
Emile Cormier

Reputation: 29209

Check out the Boost.Serialization library at www.booost.org. It knows how to read and write STL collections to/from files. I don't know if it can handle nested containers, though.

You may also want to use Boost.Multiarray for your 3-dimensional data. If you're going to do matrix math on your data, then you might want to use Boost.uBlas.

Upvotes: 0

Ioan
Ioan

Reputation: 2412

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.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490118

Since you're writing binary data (and apparently working under Windows) you really need to specify ios::binary when you open the fstream.

Upvotes: 3

Related Questions