Concept of a file
A file is an external collection of related data treated as a unit. Since the
content of primary memory is lost when the computer is shut down, we need
files to store our data in a more permanent form.
Since files exist separately from the computer and our programs, we must
find some way for our program to connect to these files. A standard data
type named FILE has been defined in the standard input/output library. This
standard type is used to define variable of appropriate type for reading from
or writing to files.
The format for the file type is shown below:
FILE *filename;
Standard library input/output file functions:
In order to be able to connect to a file we need to do the following:
1. Open a file
2. Close a file
3. Read from a file
3.1. Reading character by character
3.2. Reading formats specified by programmer
4. Writing to a file
4.1. Writing character by character
4.2. Writing in formats specified by programmer
5. Check for end of file conditions
To open a file:
The function that prepares a file for processing is fopen.
The prototype of this function is in stdio.h and is shown below:
FILE *fopen(“filename”, “mode”);
1
fopen opens the file named by filename and associates a stream with it.
fopen returns a pointer to be used to identify the stream in subsequent
operations.
The mode string used in calls to fopen is one of the following values:
Value Description
r Open for reading only.
w Create for writing. If a file by that name already exists, it will be
overwritten.
a Append; open for writing at end-of-file or create for writing if the
file does not exist.
r+ Open an existing file for update (reading and writing).
w+ Create a new file for update (reading and writing). If a file by that
name already exists, it will be overwritten.
a+ Open for append; open (or create if the file does not exist) for
update at the end of the file.
On successful completion fopen returns a pointer to the newly opened
stream. In the event of error it returns NULL.
To close a file
The function that closes the file is fclose.
The prototype of this function is in stdio.h and is shown below:
int fclose(FILE *filename);
fclose closes the named filename. All memory contents that are related to
the file are written to the disk before closing.
fclose returns 0 on success. It returns EOF if any errors were detected.
Reading character by character
The function that reads the file character by character is fgetc.
The prototype of this function is in stdio.h and is shown below:
int fgetc(FILE *filename);
On success fgetc returns the character read after converting it to an int
without sign extension. On end-of-file or error it returns EOF.
2
Reading formats specified by programmer
The function that reads the file according to the format specified by the
programmer is fscanf.
The prototype of this function is in stdio.h and is shown below:
int fscanf(FILE *filename, “format specifier”[, address, ...]);
fscanf scans a series of input fields one character at a time reading from a
stream. Then each field is formatted according to a format specifier passed
to fscanf in the format string pointed to by format. Finally fscanf stores the
formatted input at an address passed to it as an argument following format.
The number of format specifiers and addresses must be the same as the
number of input fields.
Note: For details on format specifiers, see scanf Format Specifiers described
in previous lectures.
fscanf can stop scanning a particular field before it reaches the normal end-
of-field character (whitespace) or it can terminate entirely for a number of
reasons.
fscanf returns the number of input fields successfully scanned, converted
and stored. The return value does not include scanned fields that were not
stored.
If fscanf attempts to read at end-of-file, the return value is EOF. If no fields
were stored, the return value is 0.
Writing character by character
The function that writes to a file character by character is fputc.
The prototype of this function is in stdio.h and is as follows:
int fputc(int c, FILE *filename);
On success, fputc returns the character c. On error, it returns EOF.
3
Writing in formats specified by programmer
The function that writes to the file according to the format specified by the
programmer is fprintf.
The prototype of this function is in stdio.h and is shown below:
int fprintf(FILE *filename, “format specifier”[, argument, ...]);
fprintf accepts a series of arguments applies to each a format specifier
contained in the format string pointed to by format and outputs the formatted
data to a stream. There must be the same number of format specifiers as
arguments.
Note: For details on format specifiers, see printf Format Specifiers in
previous lectures.
fprintf returns the number of bytes output. In the event of error it returns
EOF.
Check for end of file conditions
The function that detects the end of file is feof.
The prototype of this function is in stdio.h and is as follows:
int feof(FILE *filename);
feof returns nonzero if an end-of-file indicator was detected on the last input
operation on the named file and 0 if end-of-file has not been reached.
Example for doing formatted output into a new file:
#include
int main(void)
{
FILE *filename;
int i;
char c = 'A';
float f = 1.234;
filename = fopen("DUMMY.FIL", "w+"); // open a file for update
for ( i=65 ; i
int main(void)
{
FILE *in, *out;
if ((in = fopen("myfile.c", "rt")) = = NULL)
{
printf("Cannot open input file.\n");
return 1;
}
if ((out = fopen("myfile.bak", "wt")) = = NULL)
{
printf("Cannot open output file.\n");
return 1;
}
while (!feof(in))
fputc(fgetc(in), out);
fclose(in);
fclose(out);
return 0;
}
5