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

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

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 ?

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

Bestseller

C Programming Language, 2nd Edition 2nd Edition

The authors present the complete guide to ANSI standard C language programming. Written by the developers of C, this new version helps readers keep up with the finalized ANSI standard for C while showing how to take advantage of C’s rich set of operators, economy of expression, improved control flow, and data structures. The 2/E has been completely rewritten with additional examples and problem sets to clarify the implementation of difficult language constructs. For years, C programmers have let K&R guide them to building well-structured and efficient programs. Now this same help is available to those working with ANSI compilers. Includes detailed coverage of the C language plus the official C language reference manual for at-a-glance help with syntax notation, declarations, ANSI changes, scope rules, and the list goes on and on.