File Read Functions< Back to File Open Modes | Forward to The Input Files >C provides four functions that can be used to read files from disk: < Back to File Open Modes | ^ Up to Top | Forward to The Input Files >fscanf()fscanf() is a field oriented function and is inappropriate for use in a robust, general-purpose text file reader. It has two major drawbacks:
For these reasons, I chose not to include fscanf() in this presentation. The program included here does, however, have the ability to read text files using fscanf(). If you download and compile it, you can demonstrate the limitations for yourself. < Back to File Open Modes | ^ Up to Top | Forward to The Input Files >fgets()fgets() is a line oriented function. The ANSI prototype is: char *fgets(char *s, int n, FILE *stream); The function reads from the stream pointed to by stream and places the output into the character array pointed to by s. It will stop reading when any of the following conditions are true:
fgets() automatically appends a null-terminator to the data read. On the surface, fgets() seems like the ideal function to read a text file line-by-line, but as the program output shows, it is not without limitations. < Back to File Open Modes | ^ Up to Top | Forward to The Input Files >fgetc()fgetc() is a character oriented function. The ANSI prototype is: int fgetc(FILE *stream); The function returns the next character in the stream pointed to by stream as an unsigned char converted to an int. It will stop reading at the end of file or if a read error occurs. fgetc() gives you more control than fgets(), but reading a file byte-by-byte from disk is rather inefficient. < Back to File Open Modes | ^ Up to Top | Forward to The Input Files >fread()fread() is a block oriented function. The ANSI prototype is:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); The function reads from the stream pointed to by stream and places the output into the array pointed to by ptr. It will stop reading when any of the following conditions are true:
fread() gives you as much control as fgetc(), and has the advantage of being able to read more than one character in a single I/O operation. In fact, memory permitting, you can read the entire file into an array and do all of your processing in memory. This has significant performance advantages. fread() is often used to read fixed-length data records directly into structs, but you can use it to read any file. It's my personal choice for reading most disk files. The program included here demonstrates how to read an entire file into an array using fread(). It also contains an example of dynamic memory allocation to resize an array so that it can accommodate the whole file without knowing the file's size in advance. < Back to File Open Modes | ^ Up to Top | Forward to The Input Files > |