Programs to understand File handling operations.
File handling : File handling in C refers to the task of storing data in the form of input or output produced by running C programs in data files, namely, a text file or a binary file for future reference and analysis.File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file.
- Creation of the new file
- Opening an existing file
- Reading from the file
- Writing to the file
- Deleting the file
Functions of file handling :
Function | Description |
---|
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 |
---|
How to Create a File:
Syntax :
FILE *fp;
fp = fopen ("file_name", "mode");
fp is a file pointer which points to the type file.fopen is a standard function which is used to open a file.
- If the file is not present on the system, then it is created and then opened.
- If a file is already present on the system, then it is directly opened using this function.
fopen() :
Syntax : FILE *fopen( const char * filename, const char * mode );
The fopen() function accepts two parameters:
- The file name (string). If the file is stored at some specific location, then we must mention the path at which the file is stored.
- The mode in which the file is to be opened. It is a string
modes in the fopen() function.
Mode | Description |
---|
r | opens a text file in read mode |
---|
w | opens a text file in write mode |
---|
a | opens 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 |
---|
rb | opens a binary file in read mode |
---|
wb | opens a binary file in write mode |
---|
ab | opens 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 |
---|
fclose() : The fclose() function is used to close a file. The file must be closed after performing all the operations on it.
Syntax:int fclose( FILE *fp );
Example implementing fopen() and fclose() :
#include <stdlib.h> #include <stdio.h> void main() { FILE *fp; char ch; fp = fopen ("C:\\Users\\HP\\Desktop\\abc.txt", "r"); while ( 1 ) { ch = fgetc(fp ) ; if ( ch == EOF ) break ; printf("%c",ch) ; } fclose (fp ) ; } |
---|
Output :
GoEduHub Welcomes you!!
We provide free online tutorials on ML,DL,AI,Bigdata.
We provide Content of labs for RTU/JECRC University/Manipal University etc.
fprintf() : The fprintf() function is used to write set of characters into file. It sends formatted output to a stream.
Syntax: int fprintf(FILE *stream, const char *format [, argument, ...])
Example :
#include <stdlib.h> #include <stdio.h> void main() { FILE *fp; char ch; fp = fopen("C:\\Users\\HP\\Desktop\\abc.txt", "w");//opening file fprintf(fp, "implementation of fprintf...\n");//writing data into file while ( 1 ) fclose(fp); } |
---|
fscanf() :The fscanf() function is used to read set of characters from file. It reads a word from the file and returns EOF at the end of file.
Syntax: int fscanf(FILE *stream, const char *format [, argument, ...])
Example :
#include <stdlib.h> #include <stdio.h> void main() { FILE *fp; char buff[255];//creating char array to store data of file fp = fopen("C:\\Users\\HP\\Desktop\\abc.txt", "r"); while(fscanf(fp, "%s", buff)!=EOF) { printf("%s ", buff ); } fclose(fp); } |
---|
Output file abc.txt : implementation of fscanf...
fputc() :The fputc() function is used to write a single character into file. It outputs a character to a stream.
Syntax: int fputc(int c, FILE *stream)
Example :
#include <stdlib.h> #include <stdio.h> void main() { FILE *fp; //opening file fp = fopen("C:\\Users\\HP\\Desktop\\abc.txt","w"); fputc('e',fp);//writing single character into file fclose(fp);//closing file } |
---|
Output file abc.txt :
e
For more file handling functions CLICK HERE