![]() |
C File Handling|Read|Write|Append|Functions With Examples |
C File Handling|Read|Write|Append|Functions With Examples
What is File: A file Handling in this type C Programming it can be different types of operations like(read, write, append), etc, on file and operations result is stored in a permanently.
Types of Files
Two types file in c Language
1. Text File
2. Binary File
Text File: A text file can be created simply using notepad and more Editor.Text file extension of .txt
Text file content is simple and readable.
Binary File: A binary file is not readable because it is a binary file.it is a extension (.bin).Binary file data can be stored (0 or1) form.
Same important Post in Files
👉Python String
👉Python Function
👉Python File Handling
Operations of File in C Language: A C Programming operations different types
1. Open File.
2. Reading File.
3. Writing File.
4. Appending File.
5. Close File.
Mod of File Operations.
1. r: A r Character can be used to open an existing text file for reading purposes. If the file does not exist, fopen() returns NULL.
2.r+:A r+ Character can be used to open an existing text file for reading and writing purposes. If the file does not exist, fopen() returns NULL.
3. w: A w Character can be used to open a text file for writing purposes. If it does not exist, then a new file is created. If the file exists, its contents are overwritten.
4.w+:A w+ Character can be used to open a text file for writing and reading purposes. If it does not exist, then a new file is created. If the file exists, its contents are overwritten.
5. a: A Character can be used is used to open a text file in writing appending mode. If the file does not exist, then a new file is created.
6.a+:A a+ Character can be used 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.
1. r: A r Character can be used to open an existing text file for reading purposes. If the file does not exist, fopen() returns NULL.
2.r+:A r+ Character can be used to open an existing text file for reading and writing purposes. If the file does not exist, fopen() returns NULL.
3. w: A w Character can be used to open a text file for writing purposes. If it does not exist, then a new file is created. If the file exists, its contents are overwritten.
4.w+:A w+ Character can be used to open a text file for writing and reading purposes. If it does not exist, then a new file is created. If the file exists, its contents are overwritten.
5. a: A Character can be used is used to open a text file in writing appending mode. If the file does not exist, then a new file is created.
6.a+:A a+ Character can be used 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.
Important Function in C Language File
open: A open function can be used to open a text file on a given path. It has two parameter paths (where the file is created) and a mode of operation.
scan: A fscanf function can be used to scan the text file. It acts as a scanner, it reads a set of data from a file.
fprintf: A fprintf function can be used to print information or data into the file. It has also a two-parameter file and message which is to be printed.
fclose: A close function can be the predefined function that is used to close the file.
getc: A getc function can be used to get a character from a file.
putc: A putc function can be used to write a character to a file.
open: A open function can be used to open a text file on a given path. It has two parameter paths (where the file is created) and a mode of operation.
scan: A fscanf function can be used to scan the text file. It acts as a scanner, it reads a set of data from a file.
fprintf: A fprintf function can be used to print information or data into the file. It has also a two-parameter file and message which is to be printed.
fclose: A close function can be the predefined function that is used to close the file.
getc: A getc function can be used to get a character from a file.
putc: A putc function can be used to write a character to a file.
scan: A fscanf function can be used to scan the text file. It acts as a scanner, it reads a set of data from a file.
fprintf: A fprintf function can be used to print information or data into the file. It has also a two-parameter file and message which is to be printed.
fclose: A close function can be the predefined function that is used to close the file.
getc: A getc function can be used to get a character from a file.
putc: A putc function can be used to write a character to a file.
How to Write File in C Language?
#include<stdio.h>
int main()
{
FILE *f;
f=fopen("icoderweb.txt","w");
fprintf(f, "Hello Programmer, Welcome To icoderweb Website");
fclose(fp);
}
*****OUTPUT*****
Hello Programmer, Welcome To icoderweb Website
#include<stdio.h>
int main()
{
FILE *f;
f=fopen("icoderweb.txt","w");
fprintf(f, "Hello Programmer, Welcome To icoderweb Website");
fclose(fp);
}
*****OUTPUT*****
Hello Programmer, Welcome To icoderweb Website
How To Reading File in C Language?
#include<stdio.h>
int main()
{
FILE *f;
char message[200];
f=fopen("icoderweb.txt","r");
fgets(message,200,f);
printf("%s",message);
fclose(f);
}
*****OUTPUT*****
Hello Programmer, Welcome To icoderweb Website
#include<stdio.h>
int main()
{
FILE *f;
char message[200];
f=fopen("icoderweb.txt","r");
fgets(message,200,f);
printf("%s",message);
fclose(f);
}
*****OUTPUT*****
Hello Programmer, Welcome To icoderweb Website