JOIN us on
WhatsApp Group Join Now
Telegram Join Join Now

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

Categories: C Language in hindi

File Handling in C :Examples in hindi फाइल हैंडलिंग के उदाहरण c कंप्यूटर भाषा में हिंदी में

फाइल हैंडलिंग के उदाहरण c कंप्यूटर भाषा में हिंदी में File Handling in C :Examples in hindi :-
इससे पहले के article मे , हमने file handling को पढ़ा |अब हम file handling के कुछ examples को पढेगे जिससे अप सभी file handling के सभी concepts को अची तरह समझ सके |
उदाहरण 1:
इस उदहारण मे ,file 1 मे कुछ integer values को सेव करनी है फिर उन values मे से सम ‘values’ को एक दूसरी file ‘even’ मे और विसम values को दूसरी file ‘odd’ मे saved करनी है |
Explanation :
इस प्रोग्रम मे ,यूजर से सभी data को file ‘file1’ मे saved करने के लिए putw() function का use करेगे |
जब ये operation perform हो जाये गे ,तब file file1 को close कर देगे |
फिर file1 को read mode मे open करेगे |और बाकि दोनों files ‘even’ और ‘odd’ को write mode मे open करेगे |
फिर एक एक करके file1 मे से data को read करते हुए even-odd की condition को check करेगे |
अगर even की  condition true हो जाती ,read data even file मे store हो जाता है |
अगर odd की condition true हो जाती ,तब read data odd file मे store हो जाता है |
Source Code :
#include<conio.h>
#include<stdio.h>
void main()
{
FILE *f,*e*o;                    // file pointer declaration//
int i,a[10],num;
f=fopen(“file1″,”w”);       // open file1 with write mode //
printf(“Enter Ten value “);
for (i=0;i,10;i++)
{
scanf(“%d”,&a[i]);
putw(a[i],f);                    // write command for each element of array
}
fclose(f);
f=fopen(“File1″,”r”);     // open file1with read mode //
e=fopen(“even”,”w”);   // open even file with write mode//
o=fopen(“odd”,”w”);   //  open odd file with write mode //
for (i=0;i<10;i++)
{
if(feof(f))                  // end of file Error Check//
{
num=getw(f);         // Data read form file 1//
if((num%2)==0)    // Condition check for even-odd //
{
putw(num,e);       // data write in even file //
}
else
{
putw(num,o);     // data write in odd file //
}
}
else
{
printf(“No more number left for checking”);
}
}
fclose(f);
fclose(e);
fclose(o);
e=fopen(“even”,”r”);
o=fopen(“odd”,”r”);
printf(“Values of even file “);
whie(feof(e))
{
num=getw(e);
printf(“%d”,num);
}
printf(“Values of odd file “);
while(feof(o))
{
num=getw(o);
printf(“%d”,num);
}
fclose(e);
fclose(e);
}
इस  उदहारण मे तीन file pointer (f,o,e) है जो की file1 ,odd और even को open करने के लिए किय जाता है |
आउटपुट होगा :
Enter Ten value 12 14 23 34 56 76 87 79 99 53
Values of even file 12 14 34 56 76
Values of odd file 23 87 79 99 53
उदहारण 2:
इस उदाहरण मे ,किसी file से किसी students के details को read करना है और नए students के detail को add भी करना है |
Explanation :
इस उदाहरण को हम function से solve करेगे |क्योकि इसमें दो अलग अलग operation perform होगे :
1.file मे data को save करना है |
2.उस file मे saved data को display करने के लिए|
ये दोनों operation यूजर पर निर्भर करते है |इसलिए हम यूजर से input लेकर ,फिर उसके अनुसार function को call करेगे |
जब यूजर ,display के लिए ‘D’ का option देता है तब switch statement मे add_detail() function call हो जाता है |
या जब यूजर , किसी record को file मे सेव करने के लिए “A” करता है तब display() function call हो जाता है |
add_detail() function:
इस function मे file ‘students_detail’ को append mode मे open किया गया है क्योकि जब बार बार किसी नये students के detail add होगी तब पुराने records पर कोई effect नहीं पड़ना चाहिए|
उसके बाद file error check होती है |अगर file open नहीं होती है , error message “file can not open ” occur हो जाता है |
और file open हों एके बाद ,यूजर से स्टूडेंट की detail (name,birth year,class और grade) input कराई जाती है |
इन सभी detail को fprintf() function से file मे variable Name , Year , Class और Grade मे save करा दे जाती है |
फिर fclose() function से file को close कर दे जाती है |
display()
इस function मे , file student.data  को read mode मे open किया जाता है |
फिर while loop मे feof(f)==0 की condition के साथ चलाया जाता है |feof(f)==0 condition का मतलब है की जब तक file ,student.data  के सभी record read नहीं हो जाते तब तक feof(f) की value ‘0’ आएगी और loop चलता रहेगा |
फिर fscanf() function से ,file मे store records की value variable Name,Class ,Age और Grade मे store हगो जाती है |
फिर इन variable की values को print करा देगे |
Source Code
#include<stdio.h>
#include<conio.h>
void add_detail();
void display();
void main_menu();
void main()
{
int i;
main menu();
scanf(“%c”,&choice);
while(1)
{
scanf(“%c”,&choice);
switch(choice)
{
case ‘D’:display();
break;
case ‘A’:add_detail();
break;
case ‘E’:exit(0);
Break;
}
main menu ();
}
void main menu()
{
printf(“Enter Your Choice \n”);
printf (“Enter  ‘D ‘ for display record\n”);
printf(“Enter ‘A’ for add record\n”);
printf(“Enter ‘E’ for exit\n “);
}
void add_detail()
{
FILE *f;
f=fopen(“Student.data”,”a”);
if(f==NULL)
{
printf(“File can not open “);
}
else
{
printf(“Enter Student Data”);
printf(“Student name , Birth Year , Class , Grade\n”);
fprintf(f,”Name:%s , Year=%d , Class=%d , Grade=%f”,Name,Year,Class,Grade);
}
fclose(f);
}
void display()
{
FILE *f;
char name[20];
int Age , Class ,Grade ;
f=fopen(“Student.data”,”r”);
while(feof(f)==0)
{
fscanf(f,”%s %d %f”,Name,&Year,&Class,&Grade);
printf(“Student Details”);
printf(“Student Name : %s\t\t”,Name);
printf(“Birth Year : %d\n”,Year );
printf(“Class : %d\t\t”,Class);
printf((“Grade : %f\n”Grade);
}
fclose(f);
}
आउटपुट होगा :
Enter Your Choice
Enter  ‘D ‘ for display record
Enter ‘A’ for add record
Enter ‘E’ for exit
A
Enter Student Data
Student name , Birth Year , Class , Grade
Parth 1995 7 89.00
Enter Your Choice
Enter  ‘D ‘ for display record
Enter ‘A’ for add record
Enter ‘E’ for exit
D
Student Details
Student Name : Parth       Birth Year :1995
Class : 7                            Grade : 89.00
इन दो उदहारण से ,file handling के concept को lightness मिली होगी |हमारे C Project heading के article मे सभी प्रोजेक्ट मे file handling को use किया गया है |अतः आप उन article को read करके भी file handling को सिख सकते है |
Sbistudy

Recent Posts

सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है

सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…

1 day ago

मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the

marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…

1 day ago

राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi

sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…

3 days ago

गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi

gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…

3 days ago

Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन

वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…

3 months ago

polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten

get all types and chapters polity notes pdf in hindi for upsc , SSC ,…

3 months 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