Notes: File Handling in C programming | Operations in Files

Basics of File Handling in C

 

So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. Different operations that can be performed on a file are: 
 

No.FunctionDescription
1fopen()opens a new or existing file
2fprintf()write data into the file
3fscanf()reads data from the file
4fputc()writes a character into the file
5fgetc()reads a character from the file
6fclose()closes the file
7fseek()sets the file pointer to the given position
8fputw()writes an integer to file
9fgetw()reads an integer from the file
10ftell()returns current position
11rewind()sets the file pointer to the beginning of the file

 

Lightbox
Credits: Geeks for Geeks

 

File opening modes in C: 
 

ModeDescription
rOpens an existing text file for reading purposes.
wOpens a text file for writing. If it does not exist, then a new file is created. Here your program will start writing content from the beginning of the file.
aOpens a text file for writing in appending mode. If it does not exist, then a new file is created. Here your program will start appending content in the existing file content.
r+Opens a text file for both reading and writing.
w+Opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.
a+Opens a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.
rbOpen for reading in binary mode. If the file does not exist, fopen( ) returns NULL.
wbOpen for writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
abOpen for append in binary mode. Data is added to the end of the file. If the file does not exist, it will be created.
rb+Open for both reading and writing in binary mode. If the file does not exist, fopen( ) returns NULL.
wb+Open for both reading and writing in binary mode. If the file exists, its contents are overwritten. If the file does not exist, it will be created.
ab+Open for both reading and appending in binary mode. If the file does not exist, it will be created.

 

Please find codes of 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, and MISC on the below page:

Top 100+ C Programming codes – KLE Technological University







Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!