473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem while reading a binary file

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

Jul 23 '05 #1
8 1560
On 4 Jul 2005 22:40:27 -0700, sh********@gmail.com wrote in
comp.lang.c++:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2
sh********@gmail.com wrote:
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.

Jul 23 '05 #3
I tested your code.

It worked fine!

Jul 23 '05 #4
zx***********@gmail.com wrote:
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?

--
Lionel B

Jul 23 '05 #5
Thanks. The three rules really helped. I open it in binary mode and it
really worked.

Shrish

Jul 23 '05 #6
On 5 Jul 2005 18:44:39 -0700, sh********@gmail.com wrote in
comp.lang.c++:
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!

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #7
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.
Expand|Select|Wrap|Line Numbers
  1. ifstream in("inputfile");
  2. char c[1];
  3. int count = 0;
  4. while(true)
  5. {
  6. in.read(&c[0], 1);
  7. count ++;
  8. if (in.eof() ) {
  9. cout << "end of file" << endl; ;
  10. cout << "count:" << count << endl;
  11. break;
  12. }
  13.  
  14. }
  15.  
Jul 23 '05 #8
zx***********@gmail.com wrote:
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.
Expand|Select|Wrap|Line Numbers
  1.    ifstream in("inputfile");
  2.    char c[1];
  3.    int count = 0;
  4.    while(true)
  5.    {
  6.      in.read(&c[0], 1);
  7.      count ++;
  8.      if (in.eof() ) {
  9.        cout << "end of file" << endl; ;
  10.        cout << "count:" << count << endl;
  11.        break;
  12.      }
  13.    }
  14.  


--
Lionel B

Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
6
by: | last post by:
I am rewriting a C++ application in C#. This file has a combination of Text and Binary data. I used CFile before to read the text. If I hit a certain string that denotes the following data is...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.