Thursday 14 February 2013

Opening Files in C Language



The C statements that would do this are:

#include<stdio.h>
main()
{
FILE *ptr1, *ptr2;
ptr1 = fopen ("a.dat", "r");
ptr2 = fopen ("b.dat", "w");
----
----
}

The function fopen() opens a file in the appropriate access mode.

When a file is opened, certain information regarding that file automatically gets stored in different variables. These variables are collectively classified as the data type FILE. fopen() returns a pointer to this FILE type data. Hence, the following declaration is required for defining the two pointers:

FILE *ptr1, *ptr2;

The definition of the data type FILE is provided in a standard header file called stdio.h, which is therefore included in this program.

The parameters r and w in the fopen() statements indicate the mode of access to these files.

C allows a number of modes in which a file can be opened.


1 comments:

Post a Comment

Powered by Blogger.