A Short Introduction to File I/O

To interact with a file in a C program, the programmer must perform three basic steps.

  1. Identify and open the file
  2. Perform the input or output
  3. Close the file

In order to perform these operations, you must be able to identify the file that you will be using. This is accomplished using a special declaration of the form:

FILE *fp;

where:

FILE is a special declaration type that will hold the information about the file.

It is defined by stdio.h

fp is a file pointer (as indicated by the “*”) and can be any legal identifier.

Typically, programmers use something like fp, infp, infile, outfp, outfile.

Open a file

Once the file pointer has been set up, you may start using the file. The statement to use is:

fp = fopen(filename, mode);

where:

fp is the identifier used in the FILE statement.

filename is a string that holds the name of the file on disk

This may include a path, e.g. “data.dat” or “C:\data\big.dat”

mode is a string representing how you want to open the file.

Most often you open a file for reading ("r") or writing ("w").

fopen() returns a pointer that can then be used to access the file. When the file cannot be opened (e.g., no permission, does not exist for reading), fopen() will return NULL.

The input file that we are opening for reading ("r") must already exist. In contrast, the output file we are opening for writing ("w") does not have to exist. If it doesn't, it will be created. If this output file does already exist, its previous contents will be overwritten.

Also, there are other modes you can use when opening a file, such as append ("a") to append something to the end of a file without losing its contents...or modes that allow you to both read and write.

Reading and Writing

Once a file has been successfully opened, you can read from it using fscanf() or write to it using fprintf(). These functions work just like scanf() and printf(), except they require an extra first parameter, a file pointer to file to be read/written.

Example statements:

fscanf(infile,”%i”,&value);

fprintf(outfile,”%i %i\n”, x, y);

Closing a file

When done with a file, it must be closed using the function fclose().

The statement to use is:

fclose(fp);

Closing a file is very important, especially with output files. The reason is that output is often buffered. This means that when you tell C to write something outit doesn't necessary get written to disk right away, but may end up in a buffer in memory. If you forget to close an output file, then whatever is still in the buffer may not be written out.

// Sample file I/O program

#include "stdio.h"

int main()

{

int i, x;

FILE *infp, *outfp; //declare file pointers

// Open input file and make sure that it is there

infp = fopen("x.dat","r");

if (infp == NULL)

{printf("file open error\n");

exit(1);

}

// Open output file and print header

outfp = fopen("x1.dat","w");

fprintf(outfp, "File generated by sample program\n");

// Read 10 values from input file and write values and squares

for (i=1; i<=10; i++)

{

fscanf(infp, "%i", &x);

fprintf(outfp, "%6i %8i\n", x, x*x);

}

// Close up

fclose(infp);

fclose(outfp);

}