problem while reading a binary file

S

shrishjain

Dear All,

I am having problem in reading bytes from a binary file. I read the
file in following way:

ifstream in("filename");
char c[1];
while(true) {
in.read(&c[0], 1);
if(in.eof()){
cout<<"end of file\n";
break;
}
}

The above code does not read all the file. I find that in.eof() returns
true much before the file really ends. It would be great if someone can
explain me why it is happening this way.

Thanks,
Shrish
 
J

Jack Klein

Dear All,

I am having problem in reading bytes from a binary file. I read the
file in following way:

ifstream in("filename");
char c[1];
while(true) {
in.read(&c[0], 1);
if(in.eof()){
cout<<"end of file\n";
break;
}
}

The above code does not read all the file. I find that in.eof() returns
true much before the file really ends. It would be great if someone can
explain me why it is happening this way.

Thanks,
Shrish

Here are the three most important rules for working with binary files

Rule #1: Open binary files in binary mode.

Rule #2: Don't ever forget rule #1.

Rule #3: Always remember rule #2.
 
R

Rolf Magnus

Dear All,

I am having problem in reading bytes from a binary file. I read the
file in following way:

ifstream in("filename");
char c[1];
while(true) {
in.read(&c[0], 1);
if(in.eof()){
cout<<"end of file\n";
break;
}
}

The above code does not read all the file.

How do you know that? The above code doesn't show you what it read.
 
L

Lionel B

I tested your code.

It worked fine!

I suspect you didn't test it very well... did you really test it on a *binary* (as opposed to pure text) file? What sort
of binary file? How did you check what had been read?
 
S

shrishjain

Thanks. The three rules really helped. I open it in binary mode and it
really worked.

Shrish
 
J

Jack Klein

Thanks. The three rules really helped. I open it in binary mode and it
really worked.

Shrish

With a little more practice, you will only need to use one of the
rules, instead of all 3!
 
Z

zx.zhangxiong

I tested in on both binary and pure text file. Yes, I did not test it
well.
I think it would treate some bytes of binary files as EOF. Otherwise
the test would pass.
Code:
  ifstream in("inputfile");
  char c[1];
  int count = 0;
  while(true)
  {
    in.read(&c[0], 1);
    count ++;
    if (in.eof() ) {
      cout << "end of file" << endl; ;
      cout << "count:" << count << endl;
      break;
    }

  }
 
L

Lionel B

I tested in on both binary and pure text file. Yes, I did
not test it well. I think it would treate some bytes of
binary files as EOF.

That's what I was thinking :)
Otherwise the test would pass.
Code:
ifstream in("inputfile");
char c[1];
int count = 0;
while(true)
{
in.read(&c[0], 1);
count ++;
if (in.eof() ) {
cout << "end of file" << endl; ;
cout << "count:" << count << endl;
break;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top