Steps to create a data file in C

Preview
You must first complete Data File Handling in C before viewing this Lesson



Steps to create a data file in C

  • Declaring a file pointer
  • Opening a file
  • Reading or writing data in the file
  • Closing the file

1. Declaring a file pointer

A file must be open before any I/O operation can be performed on that file. A predefined structure named File is defined in the stdio.h that contains all information about the file like name, status, buffer size, current position, end of the file status.

A file pointer is a pointer to a structure of type FILE. Whenever a file is opened, a structure of type FILE is associated with it and the file pointer that points to the Structure identifies this file. Every file you open has its own file pointer variable.

When we want to write some data to a file we need to specify the file by using its file pointer variable.

We can declare a file pointer variable as follows:

FILE *f1;

The variable f1 is the file pointer. We can use any name for file pointer but it should all naming rules of a valid identifier of C language.



2.  Opening a file

We can open a file in C language program using the fopen() function, which returns the required file pointer. If the file cannot be opened for any reason then the value NULL will be returned.

The syntax for using fopen() function is:

fopen(Filename,Mode);

Filename refers to the name of file which would be opened. The name of file must follow all the naming rules of a valid identifier of C language.

Mode refers to the mode in which we want to open the file. Various modes for opening a file in C language are as follows

         File mode                    Description
             rOpen a text file for reading.
            wCreate a text file for writing, if it exists, it is overwritten.
             aOpen a text file and append text to the end of the file.
             r+Open a text file for reading and writing.
             w+Open a text file for writing and reading.
             a+Open a text file for writing and reading.
            wbCreate a binary file for writing, if it exists, it is overwritten.
             rbOpen a binary file for reading.
            abOpen a binary file and append data to the end of the file

f1= fopen(“output_file”,”w”);

3.  Reading or writing data in the file

When a file is opened for writing i.e. if a file is opened in mode “w”, ”w+”, ”a” or “a”+.   It will be created if it does not already exist.

If the file is opened with “w” or “w+” file will always be created even if file with same name already exists.

If the file is opened with “a” or “a+” file will be created if it doesn’t exist. If it already exists the new contents would be added to the end of the file. The contents can be added to the file by using various predefined functions of C.

When a file is opened for reading i.e. if a file is opened in mode “r”, ”r+”.  The file’s contents are brought into memory and a pointer points to the very first character.

4.  Closing the file

When we have finished reading from the file, we need to close it .This is done using the function fclose( ) function. The syntax of fclose() function is:

fclose(Filepointer) ;

Filepointer refers to the pointer variable which is used to open a file.

Example:

fclose(f1) ;

Once we close the file we can no longer read from it using various file input functions like getc(), fgets() etc. unless we reopen the file.

End of file(EOF)

The file reading function  need to know  the end of file so that they can stop reading .when the end of file is reached , the operating signal  send s an end of file signal to the program. when the program receive this signal , the file reading function return EOF which is constant defined in the file  stdio.h  an its value -1 . EOF is an integer value so make sure the return value of the function is assigned to a integer variables.

 The Null Pointer or Character

NULL is a character or pointer value. If it is a pointer, then the pointer variable does not reference any object. It is used for functions which return NULL if they failed in some way.

NULL is returned by read commands of the gets family when they try to read beyond the end of an input file.

If NULL is used as a character, NULL is commonly written as ‘\0’. It is the string termination character which is automatically appended to any string in a C program. We need not bother about ‘\0′ because it is handled automatically.



Best Books of C

Lesson tags: Closing the file in c, Declaring a file pointer in c, End of file(EOF) in c, Opening a file in c, Reading or writing data in the file in c, Steps to create a data file in a C, The Null Pointer or Character in c
Back to: C Programming Language