JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

हिंदी माध्यम नोट्स

Categories: c++ language in hindi

C : File handling in computer programming , what is File handling in c language in hindi

what is File handling in c language in hindi ,
file handling ,c language के सबसे महत्वपूर्ण concept है जिसमे computer मे सेव file से data को प्रोसेस किया जाता है | इसमें char mode मुख्य होते है :
1.reading : इस mode मे file के content को read किया जाता है |
2.writing : इस mode मे ,file मे content को लिखा जाता है |
3.append : इस mode मे , file मे से data को मॉडिफाइड किया जाता है |इसके अलावा file को handle करने के लिए तीन मुख्य operations परफोर्म होता है |
1.open a file with mode
2.Operation statements
3.Closing a file

इसके अलावा error भी check की जाती है |इसके लिए निन्म function होते है | ferror और eof pointer को use किया जाता है |

इस article मे file handeling के कुछ उदाह्रानो को discuss करेगे |जिससे की reader के file handleing के concept clear हो सके |

उदहारण 1
Write a program to replace a word in file .
इस उदहारण मे , file मे उपस्थित किसी word को नए word से replace किया गया है |

Explanation
1.सबसे पहले file को read mode के साथ open किया जाता है |
2.उसके बाद एक temporary file ‘temp’ को creat किया जाता है जिसके file pointer ‘ft’ है |
3.यूजर से old word (जिसे replace करना है ) और नया word (जिससे repalce करना है ) को input करा लेते है इन्हें old और new variable मे assign कर देते है |
4.file को read किया जाता है और file के content को string मे store करा देते है |
5.फिर old को string मे find किया जाता है | और इसके position को variable ‘ref’ मे store करा देते है |
6.string को temporary file मे store करा देते है |
7.old word के first occurrence को find कर देते है |
8.जहा पर old word की first occurrence होती है यहा से string को terminate कर देते है |
9.और string और new को concentrate operation perform करा देते |
10. फिर string के remaining word को भी add कर देते है |
11.step 5 से step 10 तब तक repeat होती है जब तक की string मे old word आता है |
12.उसके बाद string को temporary file मे लिख दिया जाता है |
13.सभी file को क्लोज कर दिया जाता है |

Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define SIZE 100
void replace (char *s , const char *old ,const char *new );
void main ()
{
FILE *f;
FILE *ft;
char temp[SIZE];
char  old_word[100] ,new_word[100];
printf(“Enter Word to be replace”);
gets(old_word);
printf(“Enter Word by which you want replace”);
gets(new_word);
f=fopen(Abc.doc,”r”);
ft=fopen(temp.doc,”w”);
if (f= NULL || ft == NULL)
{
printf(“Unable to open files”);
prtintf(“Please Check File Exitense”);
return 1;
}
while (fgets(temp,SIZE,f) != NULL)
{
replace(temp,old,new);
fputs(temp,ft);
}
fclose(f);
fclose(ft);
remove(Abc.doc);
rename(“temp.doc”,”Abc.doc”);
getch();
}

void replace(char *s , char *old, char *new )
{
char *ref , temp[SIZE];
int index=0;
int length;
length=strlen(old);
while((ref=strstr(s,old))!=NULL)
{
strcpy(temp,s);
index=ref-s;
s[index]=”\0″;
strcat(str,new);
strcat(str,temp+index+length);
}
}

उदहारण 2
Write a program to rename a file .
इस उदहारण मे , किसी file के नाम को change किया गया है |

Explanation
1.सबसे पहले यूजर से file का old name input करा लेते है जिसे variable ‘old’ मे assign करा लेते है |
2.सबसे पहले यूजर से file का new name input करा लेते है जिसे variable ‘new’ मे assign करा लेते है |
3.फिर function rename() को use करते है | इसमें दो दोनों variable ‘old और ‘new’ pass होगे |
4.अगर rename () function से ‘0’ return होता है तब renaming operation successfully perform हो जाता है |
अन्यथा renaming operation , unsuccessfully perform नहीं होता है |

Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char old[100] , new[100];
printf(“Old File Name : “);
gets(old);
printf(“New File Name : “);
gets(new);
if(rename(old,new)==0)
{
printf(“Renaming Operation Preform Successfully”);
}
else
{
printf(“Renaming Operation does not Preform Successfully”);
}
getch();
}

उदहारण 3
Write a program to find properties of  a file .
इस उदहारण मे , किसी file की प्रॉपर्टीज को find  किया गया है |

Explanation
इस operation के लिए stat() function को use किया जाता है |जो की किसी file के सभी properties को find करता है |ये file की सभी property को एक structure “buf” मे store कर देता है |इस function को sys / stat.h header file मे define होता है |
1.सबसे पहले file के नाम को input करा लेते है जिसे file_name variable मे assign कर देता है |
2.अगर stat() function को call किया जाता है इसमें file name और structure को pass किया जाता है |
3.अगर stat() function की value ‘0’ होती है तब PrintProperty() function को call किया जाता है |

PrintProperty() मे ,
stats.st_mode से file के mode को find किया जाता है |
stats.st_size से file की size को find किया जाता है |
stats.st_ctime से file creation के time को find किया जाता है |
stats.st_mtime से file के modify के time को find किया जाता है |

Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char file[100] ;
struct stat detail;
printf(” File Name : “);
gets(file);
if(stat(file , &detail)==0)
{
PrintProperty(detail);
else
{
printf(“Unable to file properties”);
printf(“Please Check file name “);
}
getch();
}
void PrintProperty(struct stat detail)
{
struct time ;
printf(“File Mode : “);
if(detail.st_mode & R_OK)
printf(“Read Mode”);
if(detail.st_mode & W_OK)
printf(“Write Mode”);
if(detail.st_mode & X_OK)
printf(“Excute Mode”);

printf (“File Size : %d “, detail.st_size);
time=*(gmtime(&detail.st_ctime))
printf(“Create On data : %d-%d-%d %d:%d:%d”,time.tm_mday,time.tm_mon,time.tm_year+1900,time.tm_hour,time.tm_min,time.tm_sec);
time=*(gmtime(&detail.st_mtime))
printf(“Modified On data : %d-%d-%d %d:%d:%d”,time.tm_mday,time.tm_mon,time.tm_year+1900,time.tm_hour,time.tm_min,time.tm_sec);
}

उदहारण 4:
Write a program to check existence of  a file .

इस उदहारण मे , किसी file की existence को check किया गया है |
Explanation
किसी file के existence को तीन प्रकार से check कर सकते है |
file pointer से :
अगर file pointer की value ‘null’ होती तब file does not exits’ का message आ जाता है |
access function से,
एक्सेस function उनिस्त्द.h मे define होता है |जो की किसी file के accessibility को check करता है |इस function मे दो parameters pass होता है |(i) file name और (ii) file का mode
अगर function ‘0’ return करता है तब file requested mode को access कर सकता है | अन्यथा ‘-1’ को return करता है |
stat() से
किसी file के existence को check करने के लिए इस method को बहुत कम ही use किया जाता है |अगर किसी प्रोग्राम मे stat() function को use किया गया है तब file के existence को check किया जा सकता है |
अगर st_mode  की value F_OK से सामान होता है तब file उपस्थित है |
Source file
#include<stdio.h>
#include<conio.h>
#include<io.h>
#include<stat.h>
int fileExits(const char*file);
int fileExitsAccess(const char*file);
int fileExitsStates(const char*file);

void main ()
{
char file[100] ;
struct stat detail;
printf(” File Name : “);
gets(file);
if(fileExits(file))
{
Printf(“File Exits”);
else
{
printf(“File does Not Exits”);
}
getch();
}

int fileExits(const char*file)
{
FILE *f = fopen(file , “r”);
if(f == NULL)
{
return 0;
}
else
{
return 1;
}
fclose(f);
}
int fileExitsAccess(const char*file)
{
if(access(file,F_OK)==-1)
{
return 0;
}
else
{
return 1;
}
}
int fileExitsStates(const char*file)
{
struct stat detail;
stat(file,&detail);
if(detail.st_mode & F_OK)
{
return 1;
}
else
{
return 0;
}
}
Sbistudy

Recent Posts

मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi

malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…

4 weeks ago

कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए

राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…

4 weeks ago

हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained

hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…

4 weeks ago

तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second

Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…

4 weeks ago

चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi

chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…

1 month ago

भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi

first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…

1 month ago
All Rights ReservedView Non-AMP Version
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings
JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now