(New page: =  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 proc...)
 
 
(3 intermediate revisions by one other user not shown)
Line 1: Line 1:
=  Dealing with Files in C  =
+
[[Category:ECE264]] [[Category:Programming]] [[Category:C]]
 +
= [[ECE264]]: Dealing with Files in C  =
  
 
There are two main types of files dealt with in ECE264: binary files and text files. &nbsp;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).<br>  
 
There are two main types of files dealt with in ECE264: binary files and text files. &nbsp;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).<br>  
Line 25: Line 26:
 
===  ===
 
===  ===
  
=== Opening File ===
+
=== Opening File ===
  
To open a file, you must create a file-pointer for the system. &nbsp;You can simply do this by saying:
+
To open a file, you must create a file-pointer for the system. &nbsp;You can simply do this by saying:  
  
&nbsp;&nbsp; &nbsp; ''
+
&nbsp;&nbsp; &nbsp;''FILE *f = NULL;''  
  
Files in C are opened using the fopen command. &nbsp;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".
+
Files in C are opened using the fopen command. &nbsp;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".  
  
&nbsp;&nbsp; &nbsp;''F''''ILE *f = fopen(filename, "r"); ''
+
&nbsp;&nbsp; ''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". &nbsp;The "b" placed behind the "r" tells the system that you will be reading a binary file.
  
If you wanted to open a binary file for reading, the command is similar but the second argument changes to "rb". &nbsp;The "b" placed behind the "r" tells the system that you will be reading a binary file.
+
&nbsp;&nbsp; &nbsp;''FILE *f = fopen(filename, "rb")''
  
&nbsp;&nbsp; &nbsp;''FILE *f = fopen(filename"rb");''
+
<br>
 +
 
 +
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.
 +
 
 +
&nbsp;&nbsp; &nbsp; ''FILE *f = fopen(filename, "w");''
 +
 
 +
''&nbsp;&nbsp; &nbsp; 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.&nbsp;
 +
 
 +
Fgetc returns a single character from the file at a time, where as fgets will return a whole string of "n" numbers. &nbsp;Information on their implementation can be found here:&nbsp;[http://www.manpagez.com/man/3/fgetc/ www.manpagez.com/man/3/fgetc/]&nbsp;and&nbsp;[http://www.manpagez.com/man/3/fgets/ www.manpagez.com/man/3/fgets/]
 +
 
 +
&nbsp;&nbsp; &nbsp; &nbsp;''char letter = fgetc(filehandle);''
 +
 
 +
''&nbsp;&nbsp; &nbsp; &nbsp;fgets(destination, number_of_characters, filehandle);''
 +
 
 +
=== Reading a Binary File<br> ===
 +
 
 +
Reading from a binary file is done using the fread command. &nbsp;Implementation information can be found here:&nbsp;[http://www.manpagez.com/man/3/fread/ www.manpagez.com/man/3/fread/]&nbsp;Note that the file must have been opened for reading using the fopen command prior to the attempt to reading the file.&nbsp;
 +
 
 +
fread takes more arguments that fgetc and fgets so. &nbsp;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.&nbsp;
 +
 
 +
&nbsp;&nbsp; &nbsp; 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.
 +
 
 +
&nbsp;&nbsp; &nbsp;''int end = feof(filehandle);''<br>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.&nbsp;
 +
 
 +
=== Writing to a Text File ===
 +
 
 +
Writing to a text file can either use the fprintf command or the fputc command.&nbsp;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:&nbsp;[http://www.manpagez.com/man/3/fprintf/ www.manpagez.com/man/3/fprintf/]
 +
 
 +
&nbsp;&nbsp; &nbsp; ''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:&nbsp;[http://www.manpagez.com/man/3/fputc/ www.manpagez.com/man/3/fputc/]
 +
 
 +
&nbsp;&nbsp; &nbsp; ''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:&nbsp;[http://www.manpagez.com/man/3/fwrite/ www.manpagez.com/man/3/fwrite/]
 +
 
 +
&nbsp;&nbsp; &nbsp; ''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:
 +
 
 +
&nbsp;&nbsp; &nbsp; ''fclose(f); [where f is your filehandle from when you opened your file]''
 +
----
 +
[[2011_Spring_ECE_264_Lu|Back to ECE264, Spring 2011, Prof. Lu]]

Latest revision as of 05:27, 11 July 2012

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

Meet a recent graduate heading to Sweden for a Postdoctorate.

Christine Berkesch