| | |
What errors can occur while reading a file?
![]() |
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 )
Does the same thing apply to writing to a file?
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 )
C Syntax (Toggle Plain Text)
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 ...
C Syntax (Toggle Plain Text)
fin.seekg( 0, ios::end ) // Find file size fileSize = fin.tellg( ); if( fileSize < sizeof( int )) // Need atleast one in
>>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];
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
i = 0; while( i < count && fin >> numbers[i] ) { ++i; }
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.
>> 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
Then how do you check if the file was read correctly, i.e. if all the numbers were read correctly or not?
It is a text file, because I am creating the file in some other program.
>>this is how I would have coded it
C Syntax (Toggle Plain Text)
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?
•
•
•
•
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?
Regards Niek
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
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):
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.
C Syntax (Toggle Plain Text)
!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.
![]() |
Similar Threads
- First year assigment on reading file, sorting and outputting invoice (C++)
- Bubble sort & File output jibrish errors??? (C)
- Reading from a file to fill an array (Java)
- Error Message Concerning Reading File From A Drive (C++)
- Errors with Linux file system (*nix Hardware Configuration)
- reading a file into code (Java)
Other Threads in the C Forum
- Previous Thread: Help me quickly please
- Next Thread: question about sorting
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking histogram include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault locate logical_drives looping lowest match matrix meter microsoft motherboard mqqueue number oddnumber odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv recvblocked repetition research reversing segmentationfault sequential single socket socketprograming standard strchr string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






