most important examples of file handling in c language in hindi , c file handling examples in hindi :
file को open करने के लिए प्रोग्राम :
सबसे पहले एक file pointer को declare करते है |
FILE * f;
उसके बाद fopen() function से file को open करते है |fopen() मे file name और mode को pass किया जाता है | fopen() function से file की location को file pointer मे store हो जाती है |
f=fopen(file name ,”mode”) ;
file को close करने के लिए प्रोग्राम :
किसी file को क्लोज करने के लिए fclose() function का use किया जाता है |इस function मे file का pointer variable pass होता है |
fclose(file pointer);
Error Checking
अगर file pointer की value ‘null’ होती है तब file does not exits होती |
अगर EOF keyword का use feof को check करने के लिए किया जाता है |
इस article मे file handling के कुछ उदाहरण को discuss करेगे |जिससे की reader के file handling के concept clear हो सके |
उदहारण 1
Write a program to convert uppercase letter to lower case in file .
इस उदहारण मे , file मे
उपस्थित किसी uppercase letter को lower case में convert करना है
उपस्थित किसी uppercase letter को lower case में convert करना है
Explanation
1.सबसे पहले file को read mode के साथ open किया
जाता है |
जाता है |
2.उसके बाद एक temporary
file ‘temp’ को creat किया
जाता है जिसके file pointer
‘ft’ है |
file ‘temp’ को creat किया
जाता है जिसके file pointer
‘ft’ है |
3.file मे से character को read किया जाता है और इस character को character variable ‘c’ मे store करा देते
है |
है |
5.इसके बाद इस character को lower case मे convert कर देते है और इस converted character को temporary file मे store करा देते है |
6.step 3 से step 5 तब तक repeat है जब तक file का end नहीं हो जाता है |
7.इसके बाद temporary file का नाम file name से rename कर देते है |
8.remove file name .
9 .सभी file को क्लोज कर देते है |
Source Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void convert(FILE * f,const char *c);
void main ()
{
FILE *f;
char filename[100];
printf(“Enter File name”);
gets(filename);
f=fopen(filename,”r”);
if (f==NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
convert(f,filename)
getch();
}
void convert(FILE *f, const char *file)
{
FILE *ft;
char c;
ft=fopen(“Temp.doc”,”w”);
if (ft== NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Existent”);
fcolse(f);
return 1;
}
while((c=fgetc(f)) != EOF)
{
if(isupper(c))
{
c=tolower(c);
fputc(c,ft);
}
else
{
fputc(c,ft);
}
}
fclose(f);
fclose(ft);
remove(file);
rename(“temp.doc”,file);
}
उदहारण 2
Write a program to convert lowercase letter to uppercase in file .
इस उदहारण मे ,file मे उपस्थित किसी lowercase letter को uppercase में convert करना है |
Explanation
1.सबसे पहले file को read mode के साथ open किया जाता है |
2.उसके बाद एक temporary file ‘temp’ को create किया जाता है जिसके file pointer ‘ft’ है |
3.file मे से character को read किया जाता है और इस character को character variable ‘c’ मे store करा देते है |
5.इसके बाद इस character को uppercase मे convert कर देते है और इस converted character को temporary file मे store करा देते है |
6.step 3 से step 5 तब तक repeat है जब तक file का end नहीं हो जाता है |
7.इसके बाद temporary file का नाम file name से rename कर देते है |
8.remove file name .
9 .सभी file को क्लोज कर देते है |
Source Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void convert(FILE * f,const char *c);
void main ()
{
FILE *f;
char filename[100];
printf(“Enter File name”);
gets(filename);
f=fopen(filename,”r”);
if (f==NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
convert(f,filename)
getch();
}
void convert(FILE *f, const char *file)
{
FILE *ft;
char c;
ft=fopen(“Temp.doc”,”w”);
if (ft== NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
fcolse(f);
return 1;
}
while((c=fgetc(f)) != EOF)
{
if(islower(c))
{
c=toupper(c);
fputc(c,ft);
}
else
{
fputc(c,ft);
}
}
fclose(f);
fclose(ft);
remove(file);
rename(“temp.doc”,file);
}
उदहारण 3
Write a program to display own source code of a program .
इस उदहारण मे , किसी प्रोग्राम मे लिखे जाने वाले code को आउटपुट screen मे display किया जाता है |
Explanation
इस task के लिए , current file की location को पता लगना होता हो बाकि read and print function से task complete हो जाता है |
file की location लेन के लिए pre processor _file_को use किया जाता है |इसके अलावा कई और preprocessor होते है जैसे
-_line_ : इसका use current line के number को use करने के लिए किया जाता है |
-_DATE_ : इसका use आज की date को point करने के लिए किया जाता है |
-_TIME_ : इसका use current time को use करने के लिए किया जाता है |
-_FUNCTION_ : इसका use current function के नाम को use करने के लिए किया जाता है |
Source Code
#include<stdio.h>
#include<conio.h>
void main ()
{
FILE *f;
char ch;
f=fopen(_FILE_,”r”);
if (f==NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
while(c=fgetc(f) != EOF)
{
printf(“%c”,c);
}
getch();
}
उदहारण 4
Write a program to count occurrence of all words in file .
इस उदहारण मे ,file मे आने वाले सभी words की calculate किया जाता है |
Explanation
1.सबसे पहले file को read mode के साथ open किया जाता है |
2.उसके बाद एक string को create किया जाता है| और एक और array count[] को declare करते है जो की words के count को hold करता है |
3.file मे से word को read किया जाता है और इस word को string variable ‘word’ मे store करा देते है |
4.इसके बाद इस word को lowercase मे convert कर देते है और अगर word मे से last punctuation character को remove कर देते है |
5.इसके बाद word को file content में check किया जाता है |
अगर word exit है तब word से related count मे ‘1’ से increment हो जाता है |
7.step 3 से step 5 तब तक repeat होगी जब तक file का end नहीं हो जाता है |
Source Code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
FILE *f;
char filename[100];
char word[100];
int i, length,index, is;
char words[100][100];
int count[100];
printf(“Enter File name”);
gets(filename);
f=fopen(filename,”r”);
if (f==NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
for(i=0;i<100;i++)
{
count[i]=0;
index=0;
while(fscanf(f,”%s”,word) != EOF)
{
strlwr(word);
strlwr(word);
length=strlen(word);
if(ispunct(word[length-1]))
{
word[length-1]=’\0′;
word[length-1]=’\0′;
is=1;
for(i=0;i<index && is;i++)
{
if(is)
if(is)
{
strcmp(words[index], word)
strcmp(words[index], word)
index++;
}
else
{
count[i-1]++;
}
}
fclose(f);
printf (“word with count list “)
for(i=0;i<index;i++)
{
printf (“%s : %d”,word[i],count[i]);
}
getch();
}
उदहारण 5
Write a program to merging operation of two files .
इस उदहारण मे ,दो files को merge किया जाता है |
Explanation
1.सबसे पहले दोनों files को read mode के साथ open किया जाता है |
2.उसके बाद एक file को create किया जाता है| जिसका mode write है |
3. file1 मे से data को read किया जाता है और इस data को file 3 मे write करा देते है |
4.जब file 1 मे reading operation हो जाता है तब file 2 मे से data को read किया जाता है और इस data को file 3 मे write करा देते है |
5.इसके बाद तीनो file को close कर दिया जाता है |
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
FILE *f1 ,*f2 ,*f3;
char filename1[100];
char filename2[100];
char mergefile[100];
printf(“Enter File name 1:”);
gets(filename1);
printf(“Enter File name 2:”);
gets(filename2);
printf(“Enter merge file name :”);
gets(mergefile);
f1=fopen(filename1,”r”);
f2=fopen(filename2,”r”);
f3=fopen(mergefile,”w”);
if (f1==NULL || f2==NULL || f3==NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
while(c=fgetc(f1) != EOF)
{
fputc(c,f3);
}
while(c=fgetc(f2) != EOF)
{
fputc(c,f3);
}
fclose(f1);
fclose(f2);
fclose(f3);
getch();
}
उदहारण 6
Write a program to copying operation in files .
इस उदहारण मे ,files मे copy operation को perform किया जाता है |
Explanation
1.सबसे पहले file को read mode के साथ open किया जाता है |
2.उसके बाद एक file को create किया जाता है| जिसका mode write है |
3. file मे से data को read किया जाता है और इस data को file 2 मे write करा देते है |
4.इसके बाद दोनों files को close कर दिया जाता है |
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
FILE *f1 ,*f2;
char filename1[100];
char filename2[100];
printf(“Enter File name 1:”);
gets(filename1);
printf(“Enter File name 2:”);
gets(filename2);
f1=fopen(filename1,”r”);
f2=fopen(filename2,”w”);
if (f1==NULL || f2==NULL)
{
printf(“Unable to open files”);
prtintf(“Please Check File Existent”);
return 1;
}
while(c=fgetc(f1) != EOF)
{
fputc(c,f2);
}
fclose(f1);
fclose(f2);
getch();
}
उदहारण 7
Write a program to append operation in files .
इस उदहारण मे ,files मे copy operation को perform किया जाता है |
Explanation
1.सबसे पहले file को append mode के साथ open किया जाता है |
2.उसके बाद file मे जिस data को enter करना है use input करा लेते है |
3. इस data को file मे write करा देते है |
4.इसके बाद file को close कर दिया जाता है |
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
FILE *f1;
char filename1[100];
char data[100];
printf(“Enter File name 1:”);
gets(filename1);
printf(“Enter data :”);
gets(data );
f1=fopen(filename1,”a”);
if (f1==NULL)
{
printf(“Unable to open files”);
prtintf(“Please Check File Existent”);
return 1;
}
fputs(data,f1);
fclose(f1);
f1=fopen(filename1,”r”);
if (f1==NULL)
{
printf(“Unable to open files”);
prtintf(“Please Check File Existent”);
return 1;
}
while(c=fgets(f1) != EOF)
{
printf(“%c”,c);
}
fclose(f1);
getch();
}
उदहारण 8
Write a program to distinct even or odd number from data of files .
इस उदहारण मे ,files के data मे से even और odd number अलग अलग किया जाता है |
1.सबसे पहले दोनों files even और odd file को write mode के साथ open किया जाता है |
2.उसके बाद एक file को read mode के साथ open किया जाता है|
3. file मे से data को read किया जाता है और
अगर data ब्वें है तब इसे even file मे write करगे |
अन्यथा इसे odd file मे सेव करगे |
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main ()
{
FILE *f1 ,*even ,*odd;
char filename1[100];
char filename2[100];
char mergefile[100];
int d;
printf(“Enter File name 1:”);
gets(filename1);
printf(“Enter File name 2:”);
gets(filename2);
printf(“Enter merge file name :”);
gets(mergefile);
f1=fopen(filename1,”r”);
even=fopen(filename2,”w”);
odd=fopen(mergefile,”w”);
if (f1==NULL || f2==NULL || f3==NULL )
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
while(d=fscnaf(f1,”%d”,&d) != EOF)
{
if(d%2==0)
{
fprintf(even,”%d”,d);
}
else
{
fprintf(odd,”%d”,d);
}
fclose(f1);
fclose(even);
fclose(odd);
getch();
}