Table of Contents
- 1 File Handling in C Language
File Handling in C Language
File handling in c language plays a vital role in managing the data. Here the term managing the data means several tasks to be performed in data management.
File handling in c language is used to update, create, read, and delete a file stored on the local file system on your computer. We use several built-in functions defined in c programming to do these tasks.
This file handling in c language tutorial is written for the computer science student to make them aware of different concepts of file handling in c language.
Frequently Asked Questions
By the end of this tutorial, the student will able able to answer the following questions
- What is a file?
- What is file handling in c language?
- Knowledge of different functions used in file handling?
- What is a file pointer?
- Syntax of additional file handling functions in c language.
This tutorial covers the concepts of file handling in language. Several file handling functions in c and their use are 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 a File?
In order to understand the concepts of file handling in C Language at first, we have to understand what the file is? In the context of a computer system file is a collection of data or sequences of bytes stored on the hard disk.
In general, the file is a collection of logically related records. Each file has several attributes.
What is File Pointer?
In file handling, C language uses a structure pointer of type FILE to communicates with files. This pointer is a 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 expressed in a statement as follow –
Function for File Handling in C Language
S.No | Function Syntax | Objective of Function |
1 | fopen() | To open a new or existing file |
2 | fprintf() | To write data into the file |
3 | fscanf() | To reads data from the file |
4 | fputc() | To writes a character into the file |
5 | fgetc() | To reads a character from a file |
6 | fclose() | To closes the file |
7 | fseek() | To sets the file pointer to a given position |
8 | fputw() | To writes an integer to file |
9 | fgetw() | To reads an integer from a file |
10 | ftell() | It returns the current position |
11 | rewind() | This function sets the file pointer to the beginning of the file |
How to 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 happen using the fopen() function; this function returns the necessary file pointer.
There may be a situation. If the file does not open correctly due to some causes, then it will return 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”);
open() 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 represents the access mode.
It may be any one among the following.
“r” – we use it only for reading purposes. When the file is opened correctly, then fopen( ) loads it into memory space and sets up a pointer that tends to the first character in the file. If the file cannot be opened then this fopen( ) function returns NULL.
“w” – Searches for a given file. If the file exists, then it overwrites 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, it creates a new file with the same name. Returns NULL, if unable to open the file
How to Read Data from the 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 same operations as that of printf() and gets(), but these functions need file pointer as an 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);