Input and Output from Files

File input and output can be done using versions of printf and scanf designed for files. printf and scanf are hardcoded to send output to stdout and read input from stdin. Instead of using these streams, when reading a file you must read data from the file and store data to the file. C refers to a file using a file pointer. A file pointer is a type of variable and can be declared as follows:

    FILE * fp;  /*creates a FILE pointer named fp*/

Once a file pointer has been declared, it must be associated with a given file. This is done with the fopen command (short for file open), which either opens an existing file or creates a new file and returns a pointer to that file. fopen takes two commands, the name of the file and the status of the file. The status "w" indicates that the file is opened for writing, the status "r" indicates that the file is opened for reading and the status "a" indicates that an exisiting file is being opened and new data should be appended at the end of the file. An example of fopen is shown below.

    fp = fopen("c:/test.dat", "w");
This opens a file called test.dat which is stored in C. The file is opened for writing. Note that the fp is set equal to the file pointer returned by fopen.

If fopen fails to open the file, it will return NULL. A program should always check to make sure that the file was opened successfully. The following lines of code perform the check and simply return from the program if the file open command failed.

    if(!fp)
    {
        printf("Could not open file.  Giving up.\n");
        return 0;
    }

Once the file has been opened and a copy of the file pointer has been stored, the file can be written to using fprintf (file printf), the version of printf that is used with files. fprintf is the same as printf, except it takes as its first argument the pointer to the file. The general form is:

    fprintf(<file pointer>, <format string>, <arg1>, ..., <argN>);
and a specific example is:
    int num = 13;
    fprintf(fp, "This file contains the number %d\n", num);

One more operation must be performed when the program is done with the file. The file must be closed using the fclose command. fclose takes one argument, the pointer to the file.

    fclose(fp);

The following program demonstrates writing to a file.


#include <stdio.h>

int main(int argc, char* argv[])
{
    FILE * fp;  /*creates a FILE pointer named fp*/
	int num = 13;
	char string[] = "Samuel Beckett is a great playwright.";

    fp = fopen("c:/test.dat", "w");
    if(!fp)
    {
        printf("Could not open file.  Giving up.\n");
        return 0;
    }

    //output a string
    fprintf(fp, "This file was created with the sample output program\n");

    //output a string and the value of an int variable
    fprintf(fp, "This file contains the number %d\n", num);

    //output the value of a string variable
    fprintf(fp, "%s\n", string);

    //output the value of an int variable
    fprintf(fp, "%d\n", num);

    fclose(fp);


    return 0;
}

The file created by the program looks like this:

This file was created with the sample output program
This file contains the number 13
Samuel Beckett is a great playwright.
13

On the next page, we will look at reading from a file. next


 home                      table of contents                      about