# 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.

Learn the basics of C programming before you begin.

Contents

## 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,

"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.
"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:

## 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

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.

## 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.

Bestseller

#### The C Programming Language – 2nd Edition

The Second Edition of The C Programming Language The book enumerates the concepts related to C Programming language, as prescribed by ANSI standard. The book elaborates on the basics of procedure oriented programming and the fundamentals of writing C codes. It goes on to cover aspects like functions and loops, learning which will help novice programmers to code elementary level codes properly. Solved and unsolved questions have been provided to help programmers develop an expertise in writing C codes flawlessly.