Basics of File Handling in C

Quite often, it becomes necessary to manipulate large amounts of data, which cannot be typed manually through the keyboard. It may also be possible that the output of one program becomes an input to the other. In such cases, the data are simply stored in files. Files are what they mean in normal English – they just hold data. Each file is given a name. Instead of asking for input from the keyboard, the program simply opens the files and reads them. Similarly one can ask the program to write it into a file. Obviously, this will be a much faster & more accurate method. It also has another advantage. As we have seen above. If the output of one program is to become the input to the other, one program will write into the file and the other will read from it. Such files can also be copied into floppies and transported.

All this simply indicates that files can be extremely handled when dealing with large amounts of data. Now to the creation of files, every file is given a file name. The data is entered separately, or it can be even a blank file, into which data is written by the program.

Depending on what operation is done on the program, the file has to be “opened”. You cannot operate on the file until it is opened. A file can be opened for one of the three basic operations – to read data from it, to write data into it or to attach additional data it. There modes are called read, write and append modes respectively indicated by the letters r,w and a. The opened files are assigned to a file pointer.

See the following example:

# include<stdio.h>
FILE * infile;
Infile = fopen(“exam”, ‘r’);

What these statements do is as follows:

– The second line indicates that infile is a pointer to a file. Each file that is being used by the program should have a separate pointer.

– The next line opens a file called “ exam” in the read mode and assigns it to the infile. (fopen() is a library function used to open files and every files has to be opened in one of the three modes before it can be used). Now, we can read the contents of the file with commands similar to scanf() & printf(). The corresponding commands are fscanf & fprintf

A typical scanf command is:

fscanf(“exam”, “%d”, &marks); 

Wherein the integer marks read is stored in the variable marks. Now compare this with the format of scanf.

The difference is only fscanf instead of scanf & the filename appearing in the beginning. Otherwise, it is the same. The reason is that the compiler considers scanf as a special case of fscanf. When scanf is used, it presumes that the filename is not necessary but the input is from the standard i/o file namely the keyboard.

Similarly to write into a file, you open the file in the write mode and use appropriate fprintf statements. If you open an existing file in the write mode, the file is overwritten. i.e the existing data in the file is lost. If this is to be overcome, the file should be opened in the “append” mode. Then the new data is not written “ over” the existing data but is attached to the end of the file.

To read or write single characters, just as we have getchar & putchar in normal i/o mode, we have get c & put c in the file mode.

Each existing file will be marked with a EOE (end of file) in the end which indicates that the file has ended, and this is useful while reading the file. While writing, the EOF is automatically attached to the file. Once all operations are the file are over, it should be closed using a fclose(filename) command. A program successfully terminates only if all it’s files are closed properly.

Before we start practicing the programs one more information if a file called for writing into is not existing, the system automatically creates it. However, if a nonexistent file is called for reading, it declares an error. Now let us see a simple program that reads an input file character by character and unites into another file. The file names of both files are given at run time.

Program to copy one file to another

#include <stdio.h>
main()
{
    char in_name[25], out_name[25];
    FILE *in, *out;
    int c;
    printf(“Enter name of file to be copied: ”);
    scanf(“%24s”, in_name);
    printf(“Enter name of output file:”);
    scanf(“%24s”, out_name);
    if ( ( in = fopen (in_name, “r”)) = = (FILE *) NULL)
            printf(“Couldn’t open %s for reading.\n”,in_name);
    else if ( (out = fopen (out_name, “w”)) = = (FILE *) NULL)
            printf(“Couldn’t open %s for writing.\n”,out_name);
    else
    {
        while ( (c = getc (in)) !=EOF)
          putc (c,out);
        printf(“File has been copied.\n”);
    }
 }

OUTPUT:

Enter name of file to be copied: copyme
Enter name of output file: here
File has been copied. 
Related Post