PC Review


Reply
Thread Tools Rate Thread

Checking end of file on binary file

 
 
Chris
Guest
Posts: n/a
 
      10th Jan 2005
Hi,

What is the most easy way to check on EOF while reading a
binary file with all integers ?

In lot of examples the read data are first stored in a
string, and afterwards the string is checked on *null*
value, but that does not work for binary files.

Like this ...
string buf;
while ( (buf = file.ReadString()) != null)
{
(...)
}

I need something like
int tim;
while ( (tim = file.ReadSingle()) != null)
{
(...)
}
but that does not work, as *tim* is an integer, and cant be
checked on *null*.

Is there a separate function (so, not getting any data) in
C# for merely checking the end-of-file condition ?

Thanks,
Chris.

 
Reply With Quote
 
 
 
 
Morten Wennevik
Guest
Posts: n/a
 
      10th Jan 2005
Hi Chris,

You may be looking for BinaryReader.PeekChar()
It will return the integer value of the next character.
To force it to return the next byte instead of character
use the overloaded BinaryReader constructor specifying an
8-bit text encoding (for instance "ISO-8859-1").


On Mon, 10 Jan 2005 10:10:47 +0100, Chris <(E-Mail Removed)> wrote:

> Hi,
>
> What is the most easy way to check on EOF while reading a
> binary file with all integers ?
>
> In lot of examples the read data are first stored in a
> string, and afterwards the string is checked on *null*
> value, but that does not work for binary files.
>
> Like this ...
> string buf;
> while ( (buf = file.ReadString()) != null)
> {
> (...)
> }
>
> I need something like
> int tim;
> while ( (tim = file.ReadSingle()) != null)
> {
> (...)
> }
> but that does not work, as *tim* is an integer, and cant be
> checked on *null*.
>
> Is there a separate function (so, not getting any data) in
> C# for merely checking the end-of-file condition ?
>
> Thanks,
> Chris.
>




--
Happy Coding!
Morten Wennevik [C# MVP]
 
Reply With Quote
 
=?UTF-8?B?TWFyY2luIEdyesSZYnNraQ==?=
Guest
Posts: n/a
 
      10th Jan 2005
Hi Chris

> What is the most easy way to check on EOF while reading a
> binary file with all integers ?


In case of binary file, the easiest way is to use *Stream*:

Stream stream;// initialize it e.g.:=new FileStream("c:\\temp.bin");
byte[] intBuffer=new byte[4];
int readCounter;

do {
readCounter=stream.Read(intBuffer
, 0
, 4);
if( readCounter==4 ) {
// set your int e.g.:
// myIntTable[index++]=BitConverter.ToInt32(intBuffer, 0);
}
}
while( readCounter==4 );
// i check here if buffer is filled with wanted
// number of bytes

> In lot of examples the read data are first stored in a
> string, and afterwards the string is checked on *null*
> value, but that does not work for binary files.
>
> Like this ...
> string buf;
> while ( (buf = file.ReadString()) != null)
> {
> (...)
> }
>
> I need something like
> int tim;
> while ( (tim = file.ReadSingle()) != null)
> {
> (...)
> }
> but that does not work, as *tim* is an integer, and cant be
> checked on *null*.
>
> Is there a separate function (so, not getting any data) in
> C# for merely checking the end-of-file condition ?


Are you sure that you want to read file with integers
that were wrote in binary format?

HTH
Marcin
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change binary data buffer read from a binary file to string format Anderson Microsoft VC .NET 1 21st Jul 2006 11:35 AM
Append binary file 2 to binary file 1? vbMark Microsoft C# .NET 8 22nd Aug 2005 09:09 PM
Convert binary file->utf8->binary file Joey Lee Microsoft C# .NET 2 25th Apr 2005 07:16 AM
how to delete some bytes in a binary file,not rewrite the file. =?Utf-8?B?RmlyZVBob2VuaXg=?= Microsoft C# .NET 1 23rd Mar 2004 09:19 AM
Retrieving a binary file from a database and naminig the downloaded file explicitly James Cooke Microsoft ASP .NET 2 15th Aug 2003 10:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:17 PM.