What errors can occur while reading a file?

Reply

Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

What errors can occur while reading a file?

 
0
  #1
Dec 18th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What errors can occur while reading a file?

 
0
  #2
Dec 18th, 2006
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 86
Reputation: vicky_dev is an unknown quantity at this point 
Solved Threads: 2
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

Re: What errors can occur while reading a file?

 
0
  #3
Dec 19th, 2006
>> 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,826
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: What errors can occur while reading a file?

 
0
  #4
Dec 19th, 2006
Originally Posted by vicky_dev
The first 4 bytes( or whatever sizeof(int) is ) indicates how many numbers the file has.
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
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: What errors can occur while reading a file?

 
0
  #5
Dec 19th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: What errors can occur while reading a file?

 
0
  #6
Dec 19th, 2006
Originally Posted by vicky_dev View Post

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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC