Thursday 14 February 2013

File Handling in C Programming Language



The treatment of files in C is very simple, unlike that of other programming languages where there are special in-built and often rigid file structures and file-handling routines. C treats file input-output in much the same way as input-output from/ to the terminal, and provides file input-output functions, very similar to those for input-output from/to the terminal.

The simplicity of the input-output in C lies in the fact that it essentially treats a file as a stream of characters and accordingly allows input-output in streams of characters. Functions are available for single character as well as multiple character input-output from/to files.

This simplicity has the advantage that the programmer can read and write onto a file as he/she wishes to.

This post uses the example of a file-copy program, which copies the contents of a file called a.dat to a file called b.dat to explain file input-output.

The steps involved in copying are:

1. Opening both files, one to read from, the other to write to.

2. Reading one character from the file a.dat, writing it onto b.dat, until the end of a.dat.

3. Closing both files.

1 comments:

Post a Comment

Powered by Blogger.