WhatsApp Group Join Now
Telegram Join Join Now

c file handling examples in hindi , most important examples of file handling in c language in hindi

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 करनाहै
Explanation
1.सबसे पहले file को read mode के साथ open किया
जाता है
|
2.उसके बाद एक temporary
file ‘temp’
को creat किया
जाता है जिसके
file pointer
‘ft’
है |
3.file मे से characterकोread किया जाता है और इस characterकोcharacter variable ‘c’मे store करा देते
है
|
5.इसके बाद इसcharacter को lower case मे convert कर देते है और इस convertedcharacter को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 कर देते है और इस convertedcharacter को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 लेन के लिए preprocessor_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 occurrenceof 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को stringvariable ‘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);
length=strlen(word);
if(ispunct(word[length-1]))
{
word[length-1]=’\0′;
is=1;
for(i=0;i<index && is;i++)
{
if(is)
{
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को appendmodeके साथ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को writemodeके साथ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();
}