473,770 Members | 5,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1585
On 4 Jul 2005 22:40:27 -0700, sh********@gmai l.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #2
sh********@gmai l.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***********@g mail.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********@gmai l.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.l earn.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***********@g mail.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
9856
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
6
519
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 binary, I used the current position in the file and another stream to read to the binary data. All text data is ended with a carriage return / line feed while the binary is actually an image file listed byte by byte. Preceding the binary data...
20
3075
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: --cut here-- typedef struct pkg_ { short int info; char* data;
11
6639
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 transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
2
1864
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 (an endless loop). i thought that BinaryReader.ReadByte advanced to the next byte? i had it time out after 1000 iterations, and keeps outputting the same byte. any help appreciated, my code is below: Imports System.io
6
5273
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
3531
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
5
5165
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 of '\r' in the extracted file seems to fix the problem, but I can't find an easy solution for binary files. The code I'm using is something like: from zipfile import Zipfile z = Zipfile(open('zippedfile.zip'))
27
5124
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 Type */ unsigned char sn; /* 1-3 Serial Number */
0
9592
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.