File Handling in C
In this tutorial, we will guide you step by step on how to create a file, both empty and with data, how to read data from a new file and an existing file, and how to write data into a file. You will learn about various file modes, such as read, write, and append, and how to use them to manipulate files effectively.
We will cover all the essential concepts and syntax required to work with files in C programming language, making it easy for you to follow along and learn at your own pace. By the end of this tutorial, you will have gained the skills and knowledge needed to create, read, and write files in C programming language with ease.
So, whether you are a beginner or an experienced programmer, this tutorial will help you master file handling in C language and enhance your programming skills.
What is a file?
A file is a collection of similar types of records and is a container in a computer system for storing information.
What is File Handling in C?
File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program.
- Creating new file
- Opening new and exist file
- Read file
- Write into a file
- Delete a file
File Opening Mode:
File Functions:
- fopen() - To open a file.
- fprint() - To write into a file.
- fscanf() - To read a file.
- fclose() - To close a file
Creating a new file and Open a file:
#include<stdio.h>
#include<conio.h>
int main(){
FILE *f1;
f1 = fopen(“fileName.txt”, “w”)
}
Write into a file :
#include <stdio.h>
#include<conio.h>
main(){
FILE *f1;
f1 = fopen("file.txt", "w");//opening file
fprintf(f1, "Hello file by fprintf...\n");//writing data into file
fclose(f1);//closing file
}
Read a file:
#include <stdio.h>
#include<conio.h>
main(){
FILE *f1;
char D[255];//creating char array to store data of file
f1 = fopen("file.txt", "r");
while(fscanf(f1, "%s", D)!=EOF){
printf("%s ",D );//Print all content from the file
}
fclose(f1);
}
0 Comments