By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

End of file char for binary

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
* 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

P: n/a
* 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 discussion thread is closed

Replies have been disabled for this discussion.