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.

Advertisements

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.

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 stdio.h 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 – test.txt.

"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
Output-Reading a File

FILE

It is a file control structure that store information about FILE. The description of FILE 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

Advertisements
FILE *file_pointer;

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

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

fopen()

The function fopen() needs two arguments.

  1. file name
  2. opening mode

In the example above, the file name is test.txt and the opening mode is r which stands for read.

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.

Buffer
Figure 1- Buffer

fgetc()

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

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

ch = fgetc(file_pointer);

fclose()

Once the EOF is reached and file operation is completed, all the reference to the file and file\_ pointer must be removed from buffer. This is done using fclose();.

fclose(file_pointer);

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

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

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