file handling in c language
file handling in c language is easy and very important for future in programming , in this article we learn c language file handling with examples and full details ., before start i recommended to read useful article releted to c language...
qeutions we cover:
file:- file is a space on hard disk where data is permanent store...
-to use file handling you need a file pointer
- what is file handling in c language ?
- operations in file handling ?
- steps for file handling in c language.
file:- file is a space on hard disk where data is permanent store...
-to use file handling you need a file pointer
-file pointer declare like FILE *file-name;
-file handling is a user entered data or processed data store data in file like .txt file word file , document file or you can any store data
- in file handling you can also read and write in any file with file handling functions
-many operations in file handling
- creating a file using fopen
- open existing file with fopen
- writing in file using fputs or fprintf
- reading from existing file using fgetc or fscanf
- closing file using fclose
-fopen use to open or create a new file
syntex:- fp=fopen("filename","mode");
fclose:-
-fclose use to close a opened file
syntex :- fclose(fp);
operations in file handling in c-language
fputc --this function write a single character in file
syntax- fputc(char,FILE *fp);
fgetc -
-this function use to read characters in file
syntax- fgetc(FILE *fp);
fputs -
-this function use to write a character array in file
syntax- fputs(char str[],FILE *fp);
fgets -
-this function use to read a character array in file
syntax- fgets(char str[],int n,FILE *fp);
fprintf -
-this function use to write in file same like printf
syntax- fprintf(file *fp,"text here",variables);
fscanf -
-this function use to read from in file same like scanf
syntax- fscanf(file *fp,"format %d",variables);
-you can open file in different mode
- "r" : file open in reading mode
- "w" :file open in writing mode
- "a" : open file in appending mode
- "r+" : file open in read and write mode
- "w+" : for update
- "a+" : open file for update in end of page
how to open a file in file handling?
-first declare a file pointer variable.
-open a file assign in file pointer
-give file name and open any mode
-read or write file
-close file
statements-
FILE *fp;
fp=fopen("data.txt","r")
fputc(variable,fp);
fclose(fp);
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp; //declare file pointer
int no;
clrscr();
fp=fopen("data.txt","w"); //opening file in writing mode
do
{
scanf("%d",&no);
putw(no,fp); //write data in fp
}while(no!=0);
getch();
}
example 2- even odd with file handling
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fp1,*fp2;
int i;
clrscr();
fp=fopen("data1.txt","w");
do
{
scanf("%d",&i);
putw(i,fp);
}while(i!=0);
fclose(fp);
fp=fopen("data1.txt","r");
fp1=fopen("odd.txt","w");
fp2=fopen("even.txt","w");
do
{
i=getw(fp);
if(i%2==1)
{
putw(i,fp1);
}
else
{
putw(i,fp2);
}
}while(i!=0);
putw(-1,fp1);
putw(0,fp2);
fclose(fp1);
fclose(fp2);
fclose(fp);
fp1=fopen("odd.txt","r");
printf("\n contant of odd file");
while(i!=-1)
{
i=getw(fp1);
if(i!=-1)
{
printf("\t%d",i);
}
}
fclose(fp1);
fp2=fopen("even.txt","r");
printf("\n contant of even file");
while(i!=0)
{
i=getw(fp2);
if(i!=0)
{
printf("\t%d",i);
}
}
getch();
}
example 3:- read from existing file using getc
#include<stdio.h>
#include<conio.h>
void main( )
{
FILE *fp ;
char ch ;
clrscr();
fp = fopen("file_handle.c","r") ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf("%c",ch) ;
}
fclose (fp ) ;
getch();
}
example 4:- write a program for write and read file using fprintf and fscanf
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char name[25];
int roll,show;
float marks;
clrscr();
printf("enter 0 for write data \n enter 1 for read data");
scanf("%d",&show);
if(show==0)
{
fp=fopen("file1.txt","w");
printf("enter roll number");
scanf("%d",&roll);
printf("\n enter name");
scanf("%s",&name);
printf("\nenter marks");
scanf("%f",&marks);
fprintf(fp,"%d%s%f",roll,name,marks);
fclose(fp);
printf("\ndata writed");
clrscr();
}
if(show==1)
{
marks=0;
roll=0;
fp=fopen("file1.txt","r");
fscanf(fp,"%d%s%f",&roll,&name,&marks);
printf("\n%d\t%s\t%f",roll,name,marks);
}
getch();
}
thank you for visit....
if you like content plz share and read more article in this web... and follow us on social media.... any suggestion or help comment or msg me.... thanks
Anil Jadav.😇