File Processing C




* Storage of data in variables and arrays is temporary; all such data is lost when a program terminates

* Files are used for permanent retention of large amounts of data since they are stored on secondary storage devices, especially disk storage devices.


Files and Streams


* C views each file simply as a seqiential stream of bytes.

* Each file ends with an “end-of-file” (EOF) marker.

* When a file is opened, a stream is associated with the file.

* Three file are automatically opened:

0. standard input (stdin): data from keyboard

1. standard output (stdout): data to screen

2. standard error (stderr): data to error device (usually screen)

* Opening a file returns a pointer to a “FILE” structure (defined in

<stdio.h>) that contains information used to process the file.

* Function “fgetc”, which receives as an argument a “FILE” pointer, reads one character from that file.

* “fgetc(stdin)” is equivalent to “getchar()”.

* Function “fputc” receives as arguments a character to be written and a pointer for the file to which the character will be written.

* “fputc(‘a’, stdout)” is equivalent to “putchar(‘a’)”

* “fgets” and “fputs” can be used to read a line from a file and write a line to a file, respectively, similar to “gets” and “puts” for

“stdin” and “stdout”.


opening and closing files


Defining and opening a file:

If we want to store data in a file into the secondary memory, we must specify certain things about the file to the operating system. They include the fielname, data structure, purpose.

The general format of the function used for opening a file is


FILE *fp;



The first statement declares the variable fp as a pointer to the data type FILE. As stated earlier, File is a structure that is defined in the I/O Library. The second statement opens the file named filename and assigns an identifier to the FILE type pointer fp. This pointer, which contains all the information about the file, is subsequently used as a communication link between the system and the program.

The second statement also specifies the purpose of opening the file. The mode does this job.

R open the file for read only.

W open the file for writing only.

A open the file for appending data to it.


Consider the following statements:


FILE *p1, *p2;




In these statements the p1 and p2 are created and assigned to open the files data and results respectively the file data is opened for reading and result is opened for writing. In case the results file already exists, its contents are deleted and the files are opened as a new file. If data file does not exist error will occur.


Closing a file:

The input output library supports the function to close a file; it is in the following format.



A file must be closed as soon as all operations on it have been completed. This would close the file associated with the file pointer.

Observe the following program.


FILE *p1 *p2;

p1=fopen (“Input”,”w”);

p2=fopen (“Output”,”r”);






The above program opens two files and closes them after all operations on them are completed, once a file is closed its file pointer can be reversed on other file.

file opening modes

The mode does this job.


R open the file for read only.

W open the file for writing only.

A open the file for appending data to it.


File opening mode specifies the purpose for which programmer wants to open the file.
• C provides three basic purposes; read, write and append.
• And the basic modes area “r” for reading, “w” for writing, and “a” for appending data.
• All basic and mixed modes are tabulated below:
Mode Purpose
.                               .r Read in text mode
.                               .w Write in text mode
.                               .a Append in text mode
.                               .r+ Read + write in text mode
.                               .w+ Write + read in text mode
.                               .a+ Append + read in text mode
.                               .rb Reade in binary mode
.                               .wb Write in binary mode
.                               .ab Append in binary mode
.                               .rb+ Read + write in binary mode
.                               .wb+ Write + read in binary mode
.                               .ab+ Append + read in binary mode
• The file opening mode to be used depends upon requirement and “whether the file already exists?”
• Some important situations are given below.
Mode File already exist File does not exist
r Read -
w Overwrite Create
a Append Create



Text Mode:

Text mode is used only for text files. When a file is opened in text mode, some characters may be “hidden” from your program. The characters are still in the file, your program just can’t “see” them. You can think of this as a “filter” between the file and your program.

The ANSI standard for C does not specify what filtering should take place when a file is opened in text mode; it’s compiler-dependent. For compilers used in the DOS/Windows environment, a file opened in text mode will normally have its carriage-returns filtered out. This has the effect of translating the CR/LF pairs into single LFs. As the program output shows, different compilers implement this translation differently.

The following code fragment will open the file afile.txt for reading in text mode:

  FILE *inputFilePtr;
  inputFilePtr = fopen("afile.txt", "r");

Note that the second argument to fopen() is just an “r” (without the “b” that specifies a binary open).




binary mode

In binary mode, your program can access every byte in the file. When you use one of C’s read functions, it “sees” the file exactly as it is on disk, character-for-character. Any file may be opened in binary mode.

The following code fragment will open the file afile.txt for reading in binary mode:

  FILE *inputFilePtr;
  inputFilePtr = fopen("afile.txt", "rb");

The “r” in the second argument to fopen() opens the file for reading, and the “b” specifies binary mode.







A password will be e-mailed to you.

Feedback Form

[contact-form-7 id="98" title="Feedback Form"]