Ad Code

Mastering File Handling in C Programming Language: A Step-by-Step Guide.

 File Handling in C

Welcome to this tutorial where you will learn how to create, read, and write files in the C programming language.

Files are an essential component of many programming tasks, and being able to manipulate them efficiently is a valuable skill for any programmer. In C language, you can easily create, open, read, write, and close files using built-in functions.

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.

Operations can be performed on a file.

  • Creating new file
  • Opening new and exist file
  • Read file
  • Write into a file
  • Delete a file


File Opening Mode:

"r" - when reading a file.
"w"- When you want to write in a file.


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);  
 }  



Post a Comment

0 Comments

Ad Code