Mainly two types of on which we perform operation
1.Text File2.Binary File
Text File
1.This type of file can be created using Notepad or other simple editors.
2.The extension of text file is (.txt).
3.This file contains simple text.
Binary File
1.The extension of binary file is (.bin).
2.This file contains information in the form of binary code (1 or 0) and it is not readable.
Operation on File
1.Opening of file
2.Closing of file
3.Reading from file
4.Writing into file
5.Appending data into file
Mode of Operation
File 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. |
Some important function
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. |
File Writing
Note->Before the execution of this program go to C Drive and create a temp folder if it does not exist.
File writing is the process of writing something into a file than can stored permanently.
#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???. */
File reading is the process in which we can read the content of a file.
#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??? */
Here
I am requesting if you found this post was helpful for you then let me
know by your comment in below comment box and share it with your friends.
Also you can contact me on Instagram @rajusunagar_
If
you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other.
Thank you!!.......
0 Comments