Some content here was drawn from "C Programming, A Modern Approach" by K.N. King, Chapter 22.
Some material on this page was drawn from linuxmanpages.com
sign mantissa exp ==> 3 . 1 4 1 5 9 32 bits binary 7 chars = 56 bits
In order to read from or write to simple text files, three steps are needed:
- Open the file using fopen
- Read from the file using fscanf or write to the file using fprintf
- Close the file using fclose.
fopen
- Before a file can be used, it has to be opened. One command for doing this is the system call fopen, which has the prototype:
FILE *fopen(const char *path, const char *mode);
- The path argument is simply the name of the file you wish to open, either as a "quoted string" or as a character array containing a null-byte-terminated file name.
- The mode indicates whether the file should be opened for reading or writing ( or a few other things we won't worry about right now ), and is expressed as a quoted string such as "r".
- For now we are only interested in the following modes:
- r
- Open text file for reading. The stream is positioned at the beginning of the file.
- r+
- Open for reading and writing. The stream is positioned at the beginning of the file.
- w
- Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
- w+
- Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
- a
- Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
- a+
- Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.
- The return value from fopen is of type "FILE *". To get started we only need to know the following about FILE *s:
- Create a variable of type FILE *, and store the return value from fopen in it.
- Check the return value to make sure it is not equal to NULL before using it. If fopen returns NULL, then it was unable to open the requested file for some reason.
- The FILE * returned by fopen will be needed later in order to use the file.
- There are three FILE *s pre-defined in <stdio.h> that are particularly useful:
- stdin - Standard input, normally the keyboard.
- stdout - Standard output, normally the screen. ( Although it is somewhat common to redirect stdout to a file for later use. )
- stderr - Standard error, also normally the screen. The benefit of having both stdout and stderr is that if the "results" of the program written to stdout are redirected to a file, then the messages written to stderr are still sent to the screen.
fprintf and fscanf
- fprintf and fscanf work exactly the same as printf and scanf, except they take an extra argument at the beginning, of type FILE *:
int fprintf(FILE *stream, const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
- The first argument should be a FILE * previously returned by a succesful call to fopen.
- fprintf returns the number of characters successfully printed, or a negative number if an error occurs.
- fscanf returns the number of fields that were successfuly scanned in and stored, which may be less than the number of specifiers ( % ) provided if the wrong data type was encountered, e.g. if non-numeric characters were encountered when trying to read in a number.
- fscanf returns EOF if the end of the file was encountered before the desired number of items could be scanned in.
fclose
- After a program has finished using a file, it should always close the file using:
int fclose(FILE *stream);
- Among other things, closing a file flushes buffers from RAM out to disk, and makes the file available to other programs.
Example:
char filename[ 80 ]; FILE *inputFile; int data; printf( "Please enter a file name: " ); scanf( "%s", filename ); if( ( inputFile = fopen( filename, "r" ) ) == NULL ) { fprintf( stderr, "Error- Unable to open %s\n", filename ); exit( -1 ); } if( fscanf( inputFile, "%d", &data ) != 1 ) { fprintf( stderr, "Error reading from %s\n", filename ); exit( -2 ); } printf( "Successfully read in %d from %s\n", data, filename ); fclose( inputFile ); inputFile = NULL; // Safety precaution, to prevent trying to use a closed file.