0

I am saving 100,000 integers to a binary file using:

    for(unsigned int i = 0; i < 100000; i++){
        temp = generateRand(99999);
        file.write(reinterpret_cast<const char*>(&temp),sizeof(temp));
    }

and from this file, I'm trying to read integers, and save them into a vector.

ifstream ifile;
ifile.open("test.bin",ios::binary);

ifile.seekg(0, ifile.end);
long size = ifile.tellg();
ifile.seekg(0, ifile.beg);

int restore = 0;
int count = 0;

while(ifile.tellg() < size){
    ifile.read(reinterpret_cast<char*>(&restore), sizeof(restore));
    v.push_back(restore);
    count++;
}

However it seems like I can only read 99328 integers, not 100000. I am relatively new with read/write with binary files, so can you guys help me?

7
  • what is the type of temp? what is the size of the file you get? how do you define/open file and ifile? Commented Feb 26, 2017 at 12:35
  • oh, temp is of type int. generateRand function just generates random integers. Commented Feb 26, 2017 at 12:38
  • do you close or destroy file before reading? Commented Feb 26, 2017 at 12:39
  • no. I close file and ifile at the very end. Commented Feb 26, 2017 at 12:40
  • well there's your answer) Commented Feb 26, 2017 at 12:41

2 Answers 2

2

Looks like the file object is still open when the reading commences, which results in the described behavior.

Try calling file.close() to flush the buffer and only after that initialize ifile.

Also you will find that reading the whole vector at once can considerably speed up the process.

Sign up to request clarification or add additional context in comments.

Comments

2

It's working for me. May be you forgot to use the ios::binary flag or to close the streams?

#include <vector>
#include <fstream>
#include <iostream>

using namespace std;

void write() {
  ofstream file;
  file.open("temp.data", ios::binary);
  for(unsigned int i = 0; i < 100000; i++){
    int temp = 0; // I don't know the generateRandom(...) function
    file.write(reinterpret_cast<const char*>(&temp),sizeof(temp));
  }
}

void read() {
  ifstream ifile;
  ifile.open("temp.data", ios::binary);

  ifile.seekg(0, ifile.end);
  long size = ifile.tellg();
  ifile.seekg(0, ifile.beg);

  int restore = 0;
  vector<int> v;
  while(ifile.tellg() < size){
    ifile.read(reinterpret_cast<char*>(&restore), sizeof(restore));
    v.push_back(restore);
  }

  cout << v.size() << endl;
}

int main()
{
  write();
  read();

  return 0;
}

5 Comments

Thank you. I has to file.close() to flush the buffer!
+1 - A better program structure solves the problem by automatically closing the output file when it goes out of scope.
oh so if you use functions like above--write() and read() functions, you dont explicitly have to use close() function?
Yes, when the ofstream or ifstream object is destroyed it is automatically closed too.
Thank you so much cbuchart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.