Read file content

Hi,

I want to read the content of a binary file which does not work correctly using the read method of istream because it stops reading at the first '0' byte encountered.

Using rdbuf() seems to avoid that problem but I read that it is not a very efficient way to do it.

Is there a better way?

Thank you.
ifstream::read does not stop at the first 0 byte. There must be something wrong with your code. Or did you mean the >> operator?
If no, you should post some code.
You're right the read() function doesn't stop reading at the first 0 byte.
My problem was the way I assigned the value of the character array filled by the read() function to a string. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
string fileBuffer;
ifstream fileStream;

fileStream.open(mypath, ios::binary | ios::in);

if (fileStream.is_open())
{
  fileStream.seekg(0, ios::end);
  int length = fileStream.tellg();
  fileStream.skeeg(0, ios::beg);

  char* buffer = new char[length];

  fileStream.read(buffer, length);

  fileStream.close();

  fileBuffer.append(buffer); // Wrong
  fileBuffer.assign(buffer, length); // Right

  delete[] buffer;
}
(


Makes sense? I think it does!
Topic archived. No new replies allowed.