File Handling in CIn programming, we may require some specific input data to be generated several numbers of times. Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console, and since the memory is volatile, it is impossible to recover the programmatically generated data again and again. However, if we need to do so, we may store it onto the local file system which is volatile and can be accessed every time. Here, comes the need of file handling in C. File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file.
Functions for file handlingThere are many functions in the C library to open, read, write, search and close the file. A list of file functions are given below:
Opening File: fopen()We must open a file before it can be read, write, or update. The fopen() function is used to open a file. The syntax of the fopen() is given below. The fopen() function accepts two parameters:
We can use one of the following modes in the fopen() function.
The fopen function works in the following way.
Consider the following example which opens a file in write mode. OutputThe content of the file will be printed. #include Closing File: fclose()The fclose() function is used to close a file. The file must be closed after performing all the operations on it. The syntax of fclose() function is given below: C fprintf() and fscanf()C fprintf() and fscanf() exampleC fputc() and fgetc()C fputc() and fgetc() exampleC fputs() and fgets()C fputs() and fgets() exampleC fseek()C fseek() example
Next TopicC fprintf() and fscanf()
|