Hello friends how are you, today in this blog i will teach you what is file handling, types of file, operations on file , how file handling works and many programs using file handling in a very simple way.
Let's start
👉Mainly two types of on which we perform operation
1.Text FileFile Mode | Description |
r | It opens an existing text file for reading purpose. If the file does not exist, fopen() returns NULL. |
r+ | It opens an existing text file for reading and writing purpose. If the file does not exist, fopen() returns NULL. |
w | It opens a text file for writing purpose. If it does not exist, then a new file is created. If the file exists, its contents are overwritten. |
w+ | It opens a text file for writing and reading purpose. If it does not exist, then a new file is created. If the file exists, its contents are overwritten. |
a | It is used to open a text file in writing appending mode. If the file does not exist, then a new file is created. |
a+ | It is used to open a text file in writing and reading appending mode. If the file does not exist, then a new file is created. |
Function Name | Description |
fopen |
1.It is used to open a text file on given path. 2.It has two parameter path(where file is created) and mode of operation(Ex- r for reading or w for writing). |
fscanf |
1.It is used to scan the text file. 2.It acts like an scanner ,it reads a set of data from file. |
fprintf |
1.printf is used to printf information or data on output screen. 2.fprintf is used to print information or data into the file. 3.It has also two parameter file and message which is to be printed. |
fclose |
It is a predefined function which is used to close the file. |
getc |
It is used to get a character from file. |
putc |
It is used to write a character to a file. |
#include<stdio.h> int main() { FILE *fp; fp=fopen("/temp/demo.txt","w"); fprintf(fp,"Hello friends, how are you???."); fclose(fp); } /* ### Output ### After compiling and executing this program, go to c drive , open temp folder you will see a text file(demo.txt) now open it ,the content of this file is Hello friends, how are you???. */
#include<stdio.h> int main() { FILE *fp; char msg[200]; fp=fopen("/temp/demo.txt","r");//opening the file in reading mode fgets(msg,200,fp); printf("%s",msg); fclose(fp);//closing the file } /* ### Output ### Hello friends, how are you??? */
0 Comments