What size to use for reading binary?



My binary file size is 88200000 bytes, but each element should have 2 bytes. So when I use "infile.read( block , size)" , should I be using the size of the file or number of elements (44100000)?

And should block be resized to the number of bytes or the number of elements?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  ifstream::pos_type size; 
  std::vector<int> block;

  ifstream infile(filename.c_str(), ios::in|ios::binary|ios::ate); 
  if (infile.is_open()) 
    { 

      size = infile.tellg(); 

      block.resize(size/sizeof(int));

      infile.seekg (0, ios::beg); 
      infile.read( static_cast<char*>(&block[0]), size);
      infile.close();  

      cout << "File is in memory\n"; 

> when I use infile.read(), should I be using the size of the file or number of elements (44100000)?

Number of bytes to be read (size of the file in bytes, if you want to read all of it).

> And should block be resized to the number of bytes or the number of elements?

Number of elements.

Topic archived. No new replies allowed.