There are two main types of files that a program can work with: text files and binary files. A text file contains a sequence of characters from the printable range of the ASCII code. This typically includes the ASCII values in the range
and ASCII values and , which are used for newlines and tabs. For example, a text file may contain data similar to the following:
112 48 45.01 AZ
233 71 29.53 CA
Examples of text files include those created by any text editor such as the source files containing your C programs. In a text editor like jEdit, the newline and tab characters are not visible on screen, but they are used to indicate the end of a line or indentation. Examples of text files include those created by any text editor such as the source files containing your Python or C programs or basic data files.
Writing to a Text File
To write text data to a text file, you use the fprintf
function, which is defined in the <stdio.h>
module. This function is identical to the printf
function, except that the output is written to the given text file instead of the terminal. The text file must have been opened for writing in order to print text to it.
Here, we write text strings to the output file opened in the earlier section:
fprintf(outfile, "Student Report\n");
fprintf(outfile, "--------------------------------\n");
The first argument, outfile
is the file variable containing the file pointer that was returned by the fopen
function. The remaining arguments are identical to those of the printf
function. To output other data types, a format string is required:
fprintf(outfile, "%4d %6.2f\n", idNum, avgGrade);
NOTE
|
|
The first f in the name fprintf indicates that you are writing to a file instead of standard output (i.e. the terminal). The last f indicates that you are producing formatted output.
|
Reading from a Text File
C provides several functions for reading data from text files. The specific function needed depends on the type of data being read. The fscanf
function is used to read numerical data:
int num;
float avg;
char ch;
fscanf(infile, "%d", &num);
fscanf(infile, "%f", &avg);
fscanf(infile, "%c", &ch);
This function works just like the scanf
function, except it reads from the given input file instead of from standard input. When working with mixed types in a text file, it is important to understand how the fscanf
function works. Assume the input file contains the following data where each line ends with a newline character (\n
):
100 3.45 F
200 2.89 J
284 3.27 S
When a file is opened, a file marker is set to indicate the current position within the file. This marker is moved as the contents of the file are read. Initially, the file marker is set to reference the first character in the file. In this examples, the maker refers to the 1
in 100 on the first line.
When the fscanf
function is called, it starts at the current position and proceeds as follows:
- It skips over all white space characters (tabs, spaces, new lines).
- Reads one character at a time to build a string containing a literal value of the given type.
When reading an integer (specified with a %d
), the fscanf
function will include digits or a negative sign as it builds the string. For floating-point values (%f
), it will include digits, a negative sign, a decimal place (or an E, which is used to indicate a value in scientific notation). When reading a character, it reads a single non white space character, no matter what the character is.
- It stops as soon as it reaches the end of the file, a white space character or a character that can not be part of a literal of the given type.
- It converts the string to the given type and stores it into the indicated variable.