473,396 Members | 1,768 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,396 software developers and data experts.

End of file char for binary

So from what I have read there is no end of file char like \0 (for
text) that marks the end of a binary file. Is this true? If this is
true the only way one can read a binary file is to get the size of the
binary file and read the file while the entire file hasn't fully be
read yet.

I guess another way would be to get the size of the file and use
malloc to allocate space on the heap and read the entire file to the
heap.

is there another way of doing this? For instance what if the file is
really big.. Do you use option 1 that I suggested or is there an
easier and more practical way.

J.

Feb 22 '07 #1
8 5306
Jack wrote:
So from what I have read there is no end of file char like \0 (for
text) that marks the end of a binary file. Is this true?
Yes part from the fact that \0 doesn't mark the end of a text file.
There are know special character that mark th end of any kind of file.
If this is
true the only way one can read a binary file is to get the size of the
binary file and read the file while the entire file hasn't fully be
read yet.
That's is one way to do it, but it isn't the only way.
>
I guess another way would be to get the size of the file and use
malloc to allocate space on the heap and read the entire file to the
heap.
That works as well.
>
is there another way of doing this? For instance what if the file is
really big.. Do you use option 1 that I suggested or is there an
easier and more practical way.
The usual way is simply to keep reading until a read fails. At that
point you have (most likely) hit the end of the file. For instance
char byte;
while (file.get(byte))
{
// do something with byte
}

When the while loop exits you have reached the end of the file.

john
Feb 22 '07 #2
John Harrison wrote:
char byte;
while (file.get(byte))
{
// do something with byte
}
for (char byte; file.get(byte) ; )
{
// do something with byte
}
Feb 22 '07 #3
John Harrison <jo*************@hotmail.comwrote:
The usual way is simply to keep reading until a read fails. At that
point you have (most likely) hit the end of the file. For instance
char byte;
while (file.get(byte))
{
// do something with byte
}

When the while loop exits you have reached the end of the file.
Pedantically, the loop could have exited due to some other error reading
the file. This can be checked using file.eof() *after* exiting the
loop.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Feb 22 '07 #4
red floyd wrote:
John Harrison wrote:
>char byte;
while (file.get(byte))
{
// do something with byte
}

for (char byte; file.get(byte) ; )
{
// do something with byte
}
I like that code, first time I've seen it done that way.

john
Feb 22 '07 #5
John Harrison wrote:
red floyd wrote:
>John Harrison wrote:
>>char byte;
while (file.get(byte))
{
// do something with byte
}

for (char byte; file.get(byte) ; )
{
// do something with byte
}

I like that code, first time I've seen it done that way.
Glad you liked it. The leading expanded-scope "char byte" just irritated
me for some reason.
Feb 22 '07 #6
red floyd wrote:
John Harrison wrote:
>red floyd wrote:
>>John Harrison wrote:

char byte;
while (file.get(byte))
{
// do something with byte
}
for (char byte; file.get(byte) ; )
{
// do something with byte
}

I like that code, first time I've seen it done that way.

Glad you liked it. The leading expanded-scope "char byte" just irritated
me for some reason.
What irritates me is that there isn't an equivalent construct (that I
can think of) for do/while:

char c ;
do
{
// whatever
}
while (/* some condition involving c */) ;

What would be really nice is if you could use the following:

do
{
char c ;
// whatever
}
while (/* some condition involving c */) ;

But of course 'c' is out of scope by the time you get to the while
condition.

--
Alan Johnson
Feb 23 '07 #7
* Alan Johnson:
red floyd wrote:
>John Harrison wrote:
>>red floyd wrote:
John Harrison wrote:

char byte;
while (file.get(byte))
{
// do something with byte
}
>

for (char byte; file.get(byte) ; )
{
// do something with byte
}

I like that code, first time I've seen it done that way.

Glad you liked it. The leading expanded-scope "char byte" just
irritated me for some reason.

What irritates me is that there isn't an equivalent construct (that I
can think of) for do/while:

char c ;
do
{
// whatever
}
while (/* some condition involving c */) ;

What would be really nice is if you could use the following:

do
{
char c ;
// whatever
}
while (/* some condition involving c */) ;

But of course 'c' is out of scope by the time you get to the while
condition.
#define REPEAT do { do {
#define UNTIL( cond ) } while( !(cond) ); } while( false )

:-)

No, not seriously...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 23 '07 #8
* Alf P. Steinbach:
No, not seriously...
And furthermore, it was wrong, but funny when it popped into my mind.

The proper set of macros (or as Dan Quayle would say, macroes):

#define REPEAT do{
#define UNTIL( cond ) if( !(cond) ) { continue; } } while( false )

Sort of.

Untouched by compiler's hands, guaranteed.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 23 '07 #9

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

Similar topics

3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
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 |...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
5
by: jwright | last post by:
I have decided to use a struct to collect my data. The input file is comma dilineated between almost all of the fields. Here is the code I have so far and a sample input and output file. ...
8
by: Shalaka Joshi | last post by:
Hi, I have binary file say, "test.bin". I write "FF" in the file and expect my code to read 255 for me. char * lbuf; int lreadBytes ; long lData; FILE *pFile = fopen ("c:\\testbin","rb");
4
by: nguser3552 | last post by:
Hello everyone, I'm wondering if someone out there knows how in a visual c++ console application how I can do the following, and man I've tried, it seems simple really: I need to open up any...
1
by: willakawill | last post by:
I have a very large binary file saved from a vb array with 2 dimensions; Dim arMatrix() As Byte Dim fNum As Integer ReDim arMatrix(7166, 17769) 'code here to store data from a database into...
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...
0
by: Craftkiller | last post by:
=========Program compiles on both windows and linux=========== Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.