File Handling is the storing of data in a file using a program. In C programming, the programs store results, and other data of the program to a file using file handling in C Programming Language. File handling in C enables us to read, delete, create or update the files store on the local file system through our C programming Language.

 Different operations that can be perform on a file are

  • Creation of the new file
  • Opening an existing file
  • Reading data from an existing file
  • Writing data to the file
  • Deleting the file.

Functions for file handling

FunctionDescription
fopen()opens new or existing file
fprintf()write data into the file
fscanf()reads data from the file
fputc()writes a character into the file
fgetc()reads a character from file
fclose() closes the file
fseek()sets the file pointer to given position
fputw()writes an integer to file
fgetw()reads an integer from file
ftell()returns current position
rewind()sets the file pointer to the beginning of the file

fopen()

The fopen() function is use to open a file.

Syntax

  • FILE *fopen( const char * filename, const char * mode );  

Modes in the fopen() function.

ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode

Mode = r

Example

Input value

#include <stdio.h>
int main(){
   FILE * file;
   if (file = fopen("baic_engineer.txt", "r")){
      printf("File opened successfully in read mode");
   }
   else
   printf("The file is not present and cannot create a new file using r mode");
   fclose(file);
   return 0;
}

Output value

Output value

Mode = w

Example

Input value

#include <stdio.h>
int main(){
   FILE * file;
   if (file = fopen("basicengineer.txt", "w")){
      printf("File opene success-fully in write mode or a new file is create");
   }
   else
   printf("Error");
   fclose(file);
   return 0;
}

Output value

Output value

Mode = wb

Example

Input value

#include <stdio.h>
int main(){
   FILE * file;
   if (file = fopen("basicengineer.txt", "wb")){
      printf("File opened success-fully in write in binary mode or a new file is create");
   }
   else
   printf("Error");
   fclose(file);
   return 0;
}

Output value

Output value

fscanf()

The fscanf() function is used to read character set.

Example

Input value

#include <stdio.h>  
main(){  
   FILE *fp;  
   char buff[500]; 
   fp = fopen("file.txt", "r");  
   while(fscanf(fp, "%s", buff)!=EOF){  
   printf("%s ", buff );  
   }  
   fclose(fp);  
}  

Output value

Output value

fprintf()

The fprintf() function is used to write set of characters into file.

Syntax

  • int fprintf(FILE *stream, const char *format [, argument, …])  

Example

Input value

#include <stdio.h>  
main(){  
   FILE *fp;  
   fp = fopen("hello.txt", "w");
   fprintf(fp, "Hello file by fprintf...\n");
   fclose(fp);
}  

Output value

Output value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about C Programming language please Wikipedia click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *