I the heir of all the ages, in the foremost files of time.
A file is a collection of related data. C treats a file as a series of bytes. Many files reside on disk; however, devices like terminals, printers, and magnetic tapes are also considered files.
The C library contains a large number of routines for manipulating
files. The declarations for the structures and functions used by the
file functions are stored in the standard include file <stdio.h>
. Before doing anything with
files, you must put the line:
#include <stdio.h>
at the beginning of your program.
The declaration for a file variable is:
FILE *file-variable
; /*comment
*/
For example:
#include <stdio.h> FILE *in_file; /* file containing the input data */
Before a file can be used, it must be opened using the
function fopen
. fopen
returns a pointer to the file structure
for the file. The format for fopen
is:
file-variable
= fopen(name
,mode
);
where:
is a file variable. A value of NULL
is returned on error.
is the actual name of the file (data.txt, temp.dat, etc.).
indicates if the file is to be read or written. mode is
"w"
for writing and "r"
for reading. The flag “b” can be
added to indicate a binary file. Omitting the binary flag
indicates an ASCII (text) file. (See Section 14.2 for a description
of ASCII and binary files.)
Flags can be combined. So “wb” is used to write a binary file.
The function returns a file handle that will be used in subsequent I/O operations. If there is an I/O error, then the value NULL is returned:
FILE *in_file; /* File to read */ in_file = fopen("input.txt", "r"); /* Open the input file */ if (in_file == NULL) { /* Test for error */ fprintf(stderr, "Error: Unable to input file 'input.txt' "); exit (8); }
The function fclose
will close the file. The format of fclose
is:
status
= fclose(file-variable
);
or:
fclose(file-variable
);
The variable status is zero if the
fclose
was successful or nonzero for
an error. If you don’t care about the status, the second form closes the
file and discards the return value.
C provides three pre-opened files. These are listed in Table 14-1.
File | Description |
| Standard input (open for reading) |
| Standard output (open for writing) |
| Standard error (open for writing) |
The function fgetc
reads a single character from a file. If no more data exists in the
file, the function will return the constant EOF
(EOF
is
defined in stdio.h). Note that fgetc
returns an integer, not a character.
This return is necessary because the EOF
flag must be a noncharacter value.
Example 14-1 counts the number of characters in the file input.txt.
[File: copy/copy.c] #include <stdio.h> const char FILE_NAME[] = "input.txt"; #include <stdlib.h> int main() { int count = 0; /* number of characters seen */ FILE *in_file; /* input file */ /* character or EOF flag from input */ int ch; in_file = fopen(FILE_NAME, "r"); if (in_file == NULL) { printf("Cannot open %s ", FILE_NAME); exit(8); } while (1) { ch = fgetc(in_file); if (ch == EOF) break; ++count; } printf("Number of characters in %s is %d ", FILE_NAME, count); fclose(in_file); return (0); }
A similar function, fputc
, exists for writing a single character.
Its format is:
fputc(character
,file
);
The functions fgets
and
fputs
work on one line at a time. The
format of the fgets
call is:
string_ptr
= fgets(string
,size
,file
);
where:
is equal to string if the read was successful, or NULL
if end-of-file or an error is
detected.
is a character array in which the function places the string.
is the size of the character array. fgets
reads until it gets a line
(complete with ending
) or it
reads size-1 characters. It then ends the string with a null
(