ECE264: Dealing with Files in C

There are two main types of files dealt with in ECE264: binary files and text files.  A text file contains characters and is similar to a word processing document, but usually without the formatters. A binary file, on the other hand, can contain any kind of data (from text to even images in movies) but is encoded in the binary format (which uses only 0s and 1s).

There are a few subtle differences as to how binary and text files are handled within a C Program. These differences are summarized in the chart below and discussed in further detail below.

reading writing opening
text files fgetc, fgets fprintf, fputc "r","w"
binary files fread fwrite "rb","wb"

Opening File

To open a file, you must create a file-pointer for the system.  You can simply do this by saying:

    FILE *f = NULL;

Files in C are opened using the fopen command.  Fopen requires two arguments with it. The first one is the file name that you wish to be opened, followed by a comma, and then what you want to do with the file. If you wish to read a text file (and NOT write back to the file), the second argument would simply be "r".

   F'ILE *f = fopen(filename, "r");

If you wanted to open a binary file for reading, the command is similar but the second argument changes to "rb".  The "b" placed behind the "r" tells the system that you will be reading a binary file.

    FILE *f = fopen(filename, "rb")


If you wanted to open a file for writing to, the fopen command is changed to a "w" instead of an "r". If utilizing a binary file, the appending "b" is kept onto the command.

     FILE *f = fopen(filename, "w");

     FILE *f = fopen(filename,"wb");

Note that "writing" to a file will create the file if it does not exist. If the file does exist, however, the system will erase the existing contents of the file and will then write whatever you want to it. If you want to add something to an existing file while still preserving its existing contents, you can use the "a" command to append a file, but that utilization does not fall into the purview of this class.

Reading a Text File

Reading from a text file is usually done in two ways: fgetc or fgets. 

Fgetc returns a single character from the file at a time, where as fgets will return a whole string of "n" numbers.  Information on their implementation can be found here: www.manpagez.com/man/3/fgetc/ and www.manpagez.com/man/3/fgets/

      char letter = fgetc(filehandle);

      fgets(destination, number_of_characters, filehandle);

Reading a Binary File

Reading from a binary file is done using the fread command.  Implementation information can be found here: www.manpagez.com/man/3/fread/ Note that the file must have been opened for reading using the fopen command prior to the attempt to reading the file. 

fread takes more arguments that fgetc and fgets so.  The first is the destination of the read information, the second is the size of what you're reading (usually either a character of an integer - so sizeof(int) or sizeof(char)), the third is the number of characters or integers (depending on what you said in the second arguement) you want to have read, and the final argument is the filehandle. 

     fread(*destination, sizeof(int),1,filehandle);

End of File

There is a command in C called feof that will return a 0 if the cursor in a file is NOT at the end.

    int end = feof(filehandle);
If end = 0, the end of the file has not been reached by the cursor in the file. If anything other than 0 is returned, the cursor is at the end of the file.

If the file needs to be rewound to the beginning, the rewind command may be used, however this practice is greatly frowned upon because the file should only be read from once. While this idea may not make sense with the smaller files used in this class, in the developing world where files may or may not be several gigabytes in size, rewinding a file is simply not practical. 

Writing to a Text File

Writing to a text file can either use the fprintf command or the fputc command. Note that the file must have been opened for writing using the fopen command prior to the attempt to write to the file.

The fprintf command is identical to the printf command except for it writes to a file instead of writing to the screen. Implementation information can be found here: www.manpagez.com/man/3/fprintf/

     fprintf(filehandle,stuff_to_be_written);

The fputc command follows a similar form, but only writes one character at a time. Implementation information can be found here: www.manpagez.com/man/3/fputc/

     fputc(char_to_be_written, filehandle);

Writing to a Binary File

Writing to a binary file uses the fwrite command. Note that the file must have been opened for writing using the fopen command prior to the attempt to write to the file.

The fwrite command takes several arguments to write to the file. It requires what is to be written, the size of what is to be written, the number of items it will be writing, and the filehandle. Implementation information can be found here: www.manpagez.com/man/3/fwrite/

     fwrite(values, sizeof(int), 100, filehandle);

Closing a File

Regardless of whether a file is in binary or text, the file must be closed when you are finished with it. The closing procedure uses fclose and is identical for binary and text files:

     fclose(f); [where f is your filehandle from when you opened your file]


Back to ECE264, Spring 2011, Prof. Lu

Alumni Liaison

EISL lab graduate

Mu Qiao