C File Input-Output

Most of the programs we have seen so far involve static or dynamic memory. One of the features of C programming language is the ability to read and write files.

The information is written to operating system files in binary format and later retrieved in the memory buffer to be read or written. This is done with the help of file pointers and various C library functions.

Advertisements

Learn the basics of C programming before you begin.

File Operations in C

C language allows following file operations in broader sense.

  1. Create new file.
  2. Open existing file.
  3. Read from a file.
  4. Write to a file.
  5. Move to a specific location on file (seek).
  6. Close the File.

The library functions for file operations is stored in header file.

See the following example,

Example Program – File Operations

"test.txt"
A Square has 4 equal sides, and a rectangle has 4 sides, but they are not equal.
The triangle has 3 angles and 3 sides.

We now write a program that will use – .

"Read_file.c"
#include <stdio.h>
int main()
{
     FILE  *file_pointer;
     char ch;
  
     /* use library function fopen() to open and existing file */
     file_pointer = fopen ("test.c", "r");
  
     while(1)
     {
          /*  read each character from file_pointer which is pointing to file - test.c */
          ch = fgetc(file_pointer);
       
          /*  EOF is a macro that signals end-of-file */
          if(ch == EOF)
          break;
          printf("%c", ch);
     }
  
     /* All the character has been read, close the file with library function - fclose() */
     fclose(file_pointer);
  
return 0;
}

Output:

Output-Reading a File

FILE

It is a file control structure that store information about . The description of in Turbo C++ compiler is as follows.

typedef struct {
    short            level;
    unsigned         flags;          
    char             fd;
    unsigned char    hold;          
    short            bsize;          
    unsigned char    *buffer,*curp;
    unsigned         istemp;          
    short            token;
} FILE;

This is hidden from the programmer. The command

FILE *file_pointer;

creates a pointer of type with the ability to hold file information like size, mode of opening, location, etc. provided when the file was opened using command.

Advertisements
file_pointer = fopen("test.txt", "r");

fopen()

The function needs two arguments.

  1. file name
  2. opening mode

In the example above, the file name is and the opening mode is which stands for .

What does fopen() do ?

It does three tasks.

  1. Search for the file on the disk.
  2. If found, load the file in memory buffer.
  3. Set a character pointer that points to the first character of the file in buffer.

Since too many disk operations is costly for operating systems. A buffer is needed so that all operations are performed in the buffer and later committed to the file on the disk.

Figure 1- Buffer

fgetc()

The file in open in buffer and ready for file operations. The has a character pointer that points to first character of the file in buffer.

The function starts reading each character one at a time and assign to character variable , which is printed as output.

ch = fgetc(file_pointer);

fclose()

Once the is reached and file operation is completed, all the reference to the and must be removed from buffer. This is done using .

fclose(file_pointer);

Any further operation required; you have to open the file again using function.

Advertisements

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Exit mobile version