I want to read a file which contains a list of integers. The first 4 bytes( or whatever sizeof(int) is ) indicates how many numbers the file has.
Suppose I check the file size before reading so that I know I won't encounter EOF. Do I still need to check for file read errors? Is yes, why and what would be the correct way to do it (i.e. check after each read or once at the end )

istream fin;
fin = open( "input.num", ios::in );
if( !fin )
... // Error

fin.seekg( 0, ios::end )	// Find file size
fileSize = fin.tellg( );

if( fileSize < sizeof( int ))	// Need atleast one int
...// Error	

fin.seekg( 0, ios::beg );

fin<<count;	// First int tells the number of ints in the file
// if( fin.bad( ))         // Check here too?

if( fileSize != (count+1)*sizeof(int)) // Verify file size
...// Error
	
for( i = 0; i < count; i++ )
{
	fin<<numbers[i];
//	if( fin.bad( ))         // Or shoud I use fin.fail( )? It wont be EOF
//		           // Should this be checked each time?
	...// Error		
}

// if( fin.fail( ))	// Or check error only once after reading?
	... // Error

 	...

Does the same thing apply to writing to a file?

fin.seekg( 0, ios::end )	// Find file size
fileSize = fin.tellg( );

if( fileSize < sizeof( int ))	// Need atleast one in

Is the file a binary file or text file? If you view it in Notepad or some other text editor are the numbers readable? If they are, then more than likely it is a text file which means the test you are using to see if the file contains an integer will not work. Why? because the number '1' is only one byte long, not four bytes. Use Notepad or some other text editor to write the number '1' to a file then check the file size with Explorer or some other command-line program (depending on the os you are using). The file size will be from 1 to 3 depending on whether it contains the CR, or CR/LF line terminators.

>>fin<<count; // First int tells the number of ints in the file
this is wrong. should be
fin >> count;

And the extraction operator >> is for text files, not binary files. If the file is truely binary then use this
fin.read((char *)&count, sizeof(int));

>>fin<<numbers;
again, you used the wrong operator
fin >> numbers;


for( i = 0; i < count; i++ )
{
	fin<<numbers[i];
//	if( fin.bad( ))         // Or shoud I use fin.fail( )? It wont be EOF
//		           // Should this be checked each time?
	...// Error		
}

this is how I would have coded it

i = 0;
while( i < count && fin >> numbers[i] )
{
   ++i;
}
Comments
Good~~SunnyPalSingh

>> Is the file a binary file or text file?

It is a text file, because I am creating the file in some other program.

>>this is how I would have coded it

i = 0;
while( i < count && fin >> numbers[i] )
{
   ++i;
}

Then how do you check if the file was read correctly, i.e. if all the numbers were read correctly or not?

The first 4 bytes( or whatever sizeof(int) is ) indicates how many numbers the file has.

Then how do you check if the file was read correctly, i.e. if all the numbers were read correctly or not?

You could read the first integer into a variabel (for example x), then jump in the loop which you already have and when you reach EOF compare 'i ' with 'x'. If the numbers match, the file was read correctly.

Regards Niek

But yiu have to put the integer values out ther in the file in such a way that you can guarantee a read, ie, put it on a separate line with demarcation so you can see it is not part of the list (in case you forgot to write it in the first place, for example):

!5
1
2
3
4
5

I just picked an "!" at random. If humans are going to read the file as well, try something like "linecount:5" so it doesn't confuse readers.


Then how do you check if the file was read correctly, i.e. if all the numbers were read correctly or not?

fin will fail and return null if it encounters an error or on end-of-file.

This article has been dead for over six months. Start a new discussion instead.