91-9990449935 0120-4256464 |
File Handling in CFile Handling in c language is used to open, read, write, search or close file. It is used for permanent storage. Advantage of FileIt will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium. Functions for file handlingThere are many functions in C library to open, read, write, search and close file. A list of file functions are given below:
Opening FileThe fopen() function is used to open a file. The syntax of fopen() function is given below: You can use one of the following modes in the fopen() function.
Closing FileThe fclose() function is used to close a file. The syntax of fclose() function is given below: Writing File : fprintf() functionThe fprintf() function is used to write set of characters into file. Reading File : fscanf() functionThe fscanf() function is used to read set of characters from file. Output: Data is : Hello file by fprintf... C File Example: Storing employee informationLet's see a file handling example to store employee information as entered by user from console. We are going to store id, name and salary of the employee. Output: Enter the id 1 Enter the name sonoo Enter the salary 120000 Now open file from current directory. For windows operating system, go to TC\bin directory, you will see emp.txt file. It will have following information. emp.txt Id= 1 Name= sonoo Salary= 120000
Next TopicC Preprocessor
|