SIn this article file handling function in C we give the information about file handling functions and will also see example and programs of all these in details.

file handling function in C:

fputc()-

                        Data is writing to the file with the fputc() function.

Syntax:

fputc(character,file_pointer);

Character : The character which is to be written in the file that character comes here.

File_pointer: The pointer of the file to which the file is to be written.

/* fputc File handling Function

            Single Character is writing to the file.*/

#include<stdio.h>

#include<conio.h>

int main()

{

char ch;

FILE *ptr;

clrscr();

ptr=fopen(“c:\\demo\.txt”,”w”);

if (ptr==NULL)

{

printf(“\n\n File is not opened or created”);

}

else

{

printf(“\n\n Enter single character:”);

scanf(“%c”,&ch);

fputc(ch,ptr);

fclose(ptr);

}

return 0;

}

Output:

Enter single character: a

demo file created

/* fputs File handling function in C

            The String which is to be written in the file  */

#include<stdio.h>

#include<conio.h>

int main()

{

char name[20];

FILE*ptr;

clrscr();

ptr=fopen(“c:\\demo2.txt”,”w”);

if(ptr==NULL)

{

printf(“\n\n File is not opened or created”);

}

else

{

printf(“\n\n Enter your name:”);

scanf(“%s”,name);

fputs(name,ptr);

fclose(ptr);

}

return 0;

}

Output:

Enter your name: Yashraj

demo2 file is created.

fgetc():

                        The data in the file is read with the fgetc() function.

Syntax:

fgetc(file_pointer);

File_pointer: The pointer of the file to which the file is to be read.

It is normally returns only a single character from the file. But due to feof (end of file),it keeps on printing one- be-one character until the characters in the file are exhausted.

/* fgetc() File handling function in C

            The data in the file is read with the fgetc() function

*/

#include<stdio.h>

#include<conio.h>

int main()

{

char ch;

FILE *ptr;

clrscr();

ptr=fopen(“c:\\demo2.txt”,”r”);

if(ptr==NULL)

{

printf(“\n\n File is not opened”);

}

else

{

printf(“\n\n String in the file.\n”);

while(!feof(ptr))

{

ch=getc(ptr);

printf(“%c”,ch);

}

fclose (ptr);

}

getch();

return 0;

}

Output:

String in the file.

Yashraj

fprintf():

Data is written to the file with the fprintf() function.

Syntax:

fprintf(file_pointer,”format_specifier or string”,variables);

File_pointer:

The pointer of the file to which the file is to be written.

Format Specifier or string:

As used in printf, similarly format specifier and string are used here. Both can also be used together.

Variables:

If the format Specifier is given it is a variable. If there were no format specifier, there would be no variable.

/* fprintf() File handing function in C

            Data written to the file with the fprintf() function

*/

#include<stdio.h>

#include<conio.h>

int main()

{

char name[20];

int roll;

float cgpa;

FILE*ptr;

ptr=fopen(“c:\\demo3.txt”,”w”);

if(ptr==NULL)

{

printf(“file is not opened or created”);

}

else

{

printf(“\n\n Enter student roll no: “);

scanf(“%d”,&roll);

printf(“\n Student name: “);

scanf(“%s”,name);

printf(“\n Enter student CGPA: “);

scanf(“%f”,&cgpa);

fprintf(ptr,”\n Roll:%d,\t Name:%s,\t CGPA:%f”,roll,name,cgpa);

fclose(ptr);

}

return 0;

}

Output:

Enter student roll no: 10

Student name: Rajveer

Enter student CGPA: 9.7

demo3 file is created.

fscanf():

                        This function is used to read the data in the file.

Syntax:

fscanf(file_pointer,”format-specifier or string”,& variable);

File_pointer: The pointer of the file to which the file is to be read

Format specifier or string: As used in scanf, similarly format specifier and string are used here. Both can also be used together.

Variables: If the format specifier is given, it is a variable. If there were no format specifier there would be no variables.

/*  fscanf() File hadling function in C

            This function is used to read the data in the file.

*/

#include<stdio.h>

#include<conio.h>

void main()

{

char name[20];

int roll;

float cgpa;

FILE *ptr;

char buff[255];//creating char array to store data of file

clrscr();

ptr=fopen(“c:\\demo3.txt”,”r”);

if(ptr==NULL)

{

printf(“\n\n file is not opened”);

}

else

{

while(fscanf(ptr, “%s”, buff)!=EOF)

{

printf(“\n\n%s “, buff );

}

}

fclose(ptr);

getch();

}

Output:

Roll Number: 10

Name: Rajveer

CGPA: 9.700000

fwrite() function :-

This function works to write data to the file. With its help you can write data inside the file. This function also takes four arguments. The first argument is a pointer which points to the variable whose data is to be written to the file. The second argument is the size of how much data you want to write. The third argument is the number of items to be written at one time and the fourth is the file pointer which is pointing to the file in which the data is to be written.

fread() function :-

This function works to read data from the file. With its help you can read the data kept inside the file. This function takes four arguments. The first argument is a pointer which points to the variable on which the data is to be read and stored. The second argument is the size of how much data you want to read. The third argument is the number of items to be read at one time and the fourth is the file pointer which is pointing to the file from which the data is to be read.

// fwrite() and fread() function Program:

#include< stdio.h >

#include<conio.h>

FILE *ptr;

void main()

{

char str[20]=”Have a nice day!”;

char str2[20];

clrscr();

ptr=fopen(“c:\\test.txt”,”w”);

fwrite(str,sizeof(str)-1,1,ptr);

fclose(ptr);

ptr=fopen(“c:\\test.txt”,”r”);

fread(str2,sizeof(str),1,ptr);

printf(“\n\n%s”,str2);

fclose(ptr);

getch();

}

Output:

Have a nice day!

rewind() function :-

This function resets the position of the file pointer i.e. at the start. We use this function to reset the position of the file pointer. It takes only one argument which is a file pointer pointing to a file. This function does not return anything.

Syntax :- void rewind(FILE * stream);

ftell() function :-

This function tells the current position of the file pointer. We use this function to find the current position of the file pointer. It takes only one argument which is a file pointer pointing to a file. This function returns the current position of the file pointer.

Syntax :- long ftell(FILE * stream);

// ftell Program in C

#include<stdio.h>

#include<conio.h>

FILE *ptr;

void main()

{

int length;

char str[20]=”Have a nice day!”;

char str2[20];

clrscr();

ptr=fopen(“c:\\test2.txt”,”w”);

fputs(str,ptr);

fclose(ptr);

ptr=fopen(“c:\\test2.txt”,”r”);

fgets(str2,sizeof(str2),ptr);

printf(“%s\n”,str2);

length=ftell(ptr);

printf(“File length=%d\n”,length);

rewind(ptr);

length=ftell(ptr);

printf(“File length=%d\n”,length);

fclose(ptr);

getch();

}

Output:

Have a nice day!

File length= 16

File length= 0

fseek() function :-

This function sets the file pointer pointing to a file stream at an offset from the starting point. This means if starting point is 0 and offset is 10 then it will set the file pointer from 0 to 10. We use it to read and write the file as per our convenience. This function returns int.

Syntax :- int fseek(FILE * stream, long offset, int origin);

#include< stdio.h >

#include<conio.h>

#include< string.h >

FILE *ptr;

void main()

{

int length;

char str[30]=”Hello! My name is Rajveer.”;

char str2[7]=”Yashraj”;

clrscr();

ptr=fopen(“c:\\test3.txt”,”w”);

fputs(str,ptr);

length=strlen(str2);

fseek(ptr,(ftell(ptr)-length)-1,SEEK_SET);

fputs(str2,ptr);

fclose(ptr);

getch();

}

fflush() function in File handling:

The fflush() function cleans the input or output buffer memory.

fflush() function is used exclusively for files only.

The data which is not visible or unwritten data in the file is cleaned with fflush.

Syntax for fflush()

fflush(file_pointer);

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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