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

Checking end of file on binary file

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.

Nov 16 '05 #1
2 21883
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 <An*****@work.nl> 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]
Nov 16 '05 #2
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
Nov 16 '05 #3

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

Similar topics

13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
8
by: siliconwafer | last post by:
Hi All, If I open a binary file in text mode and use text functions to read it then will I be reading numbers as characters or actual values? What if I open a text file and read it using binary...
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: ...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
5
by: Joe Van Dyk | last post by:
I have a file that looks like this: 25.000000 55.000000 23.000000 51.000000 5 8 8700 6000 <and then, at byte 128, starts binary stuff> What's the proper way to make sure that I'm reading in...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
8
by: GiBo | last post by:
Hi! Classic situation - I have to process an input stream of unknown length until a I reach its end (EOF, End Of File). How do I check for EOF? The input stream can be anything from opened file...
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
7
by: elnoire | last post by:
Greetings! I've just started learning python, so this is probably one of those obvious questions newbies ask. Is there any way in python to check if a text file is blank? What I've tried to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.