File handling in c programming
What is file ?
Use of File Pointer.
Different file handling function.
This tutorial covers the concepts of file handling in c programming. Several file handling functions in c and their use is also explained in this tutorial. This file handling in c tutorial also covers the concepts of file pointer in c, c program to read and write to a file, opening and closing a file.
What is File ?
In order to understand the concepts of file handling in c programming at first we have to understand what is file ? In context of a computer system file is a collection of data or sequence of bytes stored on hard disk.
What is File Pointer?
In file handling in c programming C programming language uses a structure pointer of type FILE to communicates with files. This pointer is file pointer in c. This type is defined within stdio.h and written as FILE *.
If we have to declare a file pointer called output_file then it is declared in a statement as follow –
Open a file
In order to read or access from file from a c program at first program must open that file before it can access it. This can be happens using the fopen() function, this function returns the necessary file pointer.
There may be a situation If the file does not open correctly due some causes then the it will returns a NULL value.
Syntax to open a file is as follow –
if ((output_file = fopen(“output_file”, “w”)) == NULL)
fprintf(stderr, “Cannot open %sn”, “output_file”);
fopen() accepts two arguments, both are strings, the first argument is the name of the file to which we want to open and the second argument is an access character, which represent the access mode.
It may be any one among the following-
“r” – we use it only for reading purpose. When the file is opened correctly then fopen( ) loads it into memory space and sets up a pointer which tends to the first character in the file. If in the file cannot be opened then this fopen( ) function returns NULL.
“w” – Searches for given file. If the file exists then it overwrite the contents. Suppose that file doesn’t exist then it creates a new file. There may be a situation when not able to open the file it simply returns NULL.
“a” – At first search the given file .If the file opens correctly fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist in this situation a it creates a new file with same name. Returns NULL, if unable to open file.
Reading from a file.
During file handling in c programming if we want to read from a file .The file read operations can be perform using two functions fscanf() or fgets() functions to reading a file in c.
Both the functions do the the same operations as that of printf() and gets() but these function need file pointer as additional parameter.
Code snippet for reading a file is as follow-
FILE * fp;
fp = fopen(“fileName.txt”, “r”);
fscanf(fp, “%s %s %s %d”, str1, str2, str3, &year);
Writing into a file
During file handling in c programming the file write operations can be performed by the functions fprintf() and fputs() with similarities to read operations. The snippet for writing to a file is as :
FILE *fp ;
fp = fopen(“fileName.txt”, “w”);
fprintf(fp, “%s %s %s %d”, “We”, “are”, “in”, 2018);
Close a file
In file handling in c programming after performing work on file when we want to close it we use fclose() function which disconnect a file pointer from the file.
Objective behind to disconnect the file pointer from the file is to make pointer available the to access any other file.
Systems have a limit on the number of files which can be open simultaneously, so it is a good idea to close a file when you have finished using it.
If files are still open when a program exits, the system will close them for you. However it is usually better to close the files properly.
EOF The End of File Marker
In file handling in c programming EOF is a character which indicates the end of a file. getc() and scanf() functions returns it when these functions try to read beyond the limit of the end of a file.
When we perform a read operation on file then to check whether or not you have reached the end of file , feof() function do this .
if (feof (“filename.txt”) ) printf ( “ABCn” ) ;
I hope that file handling in c programming tutorial will be useful for computer science students to understand the file handling concepts in c such as file handling functions in c and their use,concepts of file pointer in c, c program to read and write to a file, opening and closing a file