944,172 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1826
  • C RSS
Dec 18th, 2006
0

What errors can occur while reading a file?

Expand Post »
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 )

  1.  
  2. istream fin;
  3. fin = open( "input.num", ios::in );
  4. if( !fin )
  5. ... // Error
  6.  
  7. fin.seekg( 0, ios::end ) // Find file size
  8. fileSize = fin.tellg( );
  9.  
  10. if( fileSize < sizeof( int )) // Need atleast one int
  11. ...// Error
  12.  
  13. fin.seekg( 0, ios::beg );
  14.  
  15. fin<<count; // First int tells the number of ints in the file
  16. // if( fin.bad( )) // Check here too?
  17.  
  18. if( fileSize != (count+1)*sizeof(int)) // Verify file size
  19. ...// Error
  20.  
  21. for( i = 0; i < count; i++ )
  22. {
  23. fin<<numbers[i];
  24. // if( fin.bad( )) // Or shoud I use fin.fail( )? It wont be EOF
  25. // // Should this be checked each time?
  26. ...// Error
  27. }
  28.  
  29. // if( fin.fail( )) // Or check error only once after reading?
  30. ... // Error
  31.  
  32. ...
Does the same thing apply to writing to a file?
Similar Threads
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Dec 18th, 2006
0

Re: What errors can occur while reading a file?

  1. fin.seekg( 0, ios::end ) // Find file size
  2. fileSize = fin.tellg( );
  3.  
  4. 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[i];
again, you used the wrong operator
fin >> numbers[i];

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

this is how I would have coded it
  1. i = 0;
  2. while( i < count && fin >> numbers[i] )
  3. {
  4. ++i;
  5. }
Last edited by Ancient Dragon; Dec 18th, 2006 at 12:26 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Dec 19th, 2006
0

Re: What errors can occur while reading a file?

>> 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

  1. i = 0;
  2. while( i < count && fin >> numbers[i] )
  3. {
  4. ++i;
  5. }


Then how do you check if the file was read correctly, i.e. if all the numbers were read correctly or not?
Reputation Points: 28
Solved Threads: 2
Junior Poster in Training
vicky_dev is offline Offline
86 posts
since May 2005
Dec 19th, 2006
0

Re: What errors can occur while reading a file?

Quote originally posted by vicky_dev ...
The first 4 bytes( or whatever sizeof(int) is ) indicates how many numbers the file has.
Quote originally posted by vicky_dev ...
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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Dec 19th, 2006
0

Re: What errors can occur while reading a file?

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):
  1. !5
  2. 1
  3. 2
  4. 3
  5. 4
  6. 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.
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004
Dec 19th, 2006
0

Re: What errors can occur while reading a file?

Click to Expand / Collapse  Quote originally posted by vicky_dev ...

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help me quickly please
Next Thread in C Forum Timeline: question about sorting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC