- File Handling में data को secondary storage device(Hard disk) में permanently store किया जाता है |
- File Handling को open, close, read, write के लिए इस्तेमाल किया जाता है |
File Handling का इस्तेमाल क्यों करते है ?
Program जब Programmer बंद करता है, तब program का सारा data destroy हो जाता है | Program में data store करने के Programmer कुछ variables, arrays, structures, unions का इस्तेमाल करता है, पर ये data permanantly store नहीं होता | इसे permanantly store करने के लिए ही File Handling का इस्तेमाल होता है | File Handling के दरम्यान create हुई files चाहे वो अलग-अलग types(.txt, .doc etc.) की हो वो portable होती है | दूसरे Computer में भी वो इस्तेमाल होती है |File Handling के Operations
File Handling के Functions को इस्तेमाल करना हो तो stdio.h इस header file को include करना पड़ता है || File Operations / Functions | Description |
|---|---|
| fopen() | File को create और open किया जाता है | |
| fclose() | Open किये हुई file को close किया जाता है | |
| fprintf() | File में data को write किया जाता है | |
| fscanf() | File में data को read किया जाता है | |
| fgetc() | File से character को read किया जाता है | |
| fputc() | File में character को write किया जाता है | |
| fseek() | दिए हुई position पर file pointer को set किया जाता है | |
| getw() | File से integer को read किया जाता है | |
| putw() | File में integer को write किया जाता है | |
| ftell() | File Pointer की current position return करता है | |
| rewind() | File Pointer को file के शुरुआत में लाता है | |
| remove() | File को remove कर देता है | |
File को open करने से पहले उनके modes को समझे |
File को open करने के लिए कुछ 'modes' होते है |
| File Modes | Description |
|---|---|
| r | read mode पे file को open किया जाता है | |
| w | write mode पे file को open किया जाता है | |
| a | append mode मतलब file में कुछ और data जोड़ने के file को open किया जाता है | |
| r+ w+ a+ | read और write modes पे file को open किया जाता है | |
| rb | read mode पे binary file को open किया जाता है | |
| wb | write mode पे binary file को open किया जाता है | |
| ab | append mode मतलब binary file में कुछ और data 'write' जोड़ने के binary file को open किया जाता है | |
| rb+ wb+ ab+ | read और write modes पे binary file को open किया जाता है | |
fopen() - File Handling
fopen() funtion से File को create और open किया जाता है |Syntax for fopen()
file_pointer = fopen("file_name", "mode_of_file");- file_pointer : ये एक file pointer है जिसका data type 'FILE' है |
- file_name : File का नाम उसके extension के साथ देना चाहिए |
for e.g. file.txt, file.docx .txt और .docx ये files के extensions है | - mode_of_file : File को किस mode से open करना ये उनका mode निश्चित करता है | File को Open करने के तीन basic modes है |
#include <stdio.h>
int main(){
FILE *fptr;
fptr = fopen("sample.txt","w");
return 0;
}
fclose() - File Handling
fclose() funtion से open किये हुए file को close किया जाता है |Syntax for fclose()
fclose(file_pointer);- file_pointer : जिस file को close करना है उस file का pointer |
#include <stdio.h>
int main(){
FILE *fptr;
fptr = fopen("sample.txt","w");
fclose(fptr);
return 0;
}
fprintf() - File Handling
fprintf() funtion से File में data को write किया जाता है |Syntax for fprintf()
fprintf(file_pointer, "format_specifier or string", variables);- file_pointer : जिस file को write करना है उस file का pointer |
- format specifier or string: जैसे printf में इस्तेमाल किया जाता है, वैसे ही यहाँ format specifier और string का इस्तेमाल यहाँ पर होता है | दोनों एक साथ भी इस्तेमाल किये जा सकते है |
- variables : अगर format specifier जहाँ पर दिया जाता है, वह पर variable तो होते ही है | अगर format specifier नहीं होते तो variables भी नहीं होते |
#include <stdio.h>
int main(){
int emp_id;
char emp_name[20];
FILE *fptr;
fptr = fopen( "sample.txt", "w" );
printf("Enter Employee ID :");
scanf("%d", &emp_id);
printf("Enter Employee Name :");
scanf("%s", &emp_name);
fprintf( fptr, "%d, %s", emp_id, emp_name ) ;
fclose(fptr);
return 0;
}
Output :
Enter Employee ID :2 Enter Employee Name :Raj
fscanf() - File Handling
fscanf() funtion से File में data को read किया जाता है |Syntax for fscanf()
fscanf(file_pointer, "format_specifier or string", &variables);- file_pointer : जिस file को read करना है उस file का pointer |
- format specifier or string: जैसे scanf में इस्तेमाल किया जाता है, वैसे ही यहाँ format specifier और string का इस्तेमाल यहाँ पर होता है | दोनों एक साथ भी इस्तेमाल किये जा सकते है |
- variables : अगर format specifier जहाँ पर दिया जाता है, वह पर variable तो होते ही है | अगर format specifier नहीं होते तो variables भी नहीं होते |
#include <stdio.h>
int main(){
int emp_id;
char emp_name[20];
FILE *fptr;
fptr = fopen( "sample.txt", "r" );
fscanf( fptr, "%d,%s", &emp_id, emp_name ) ;
printf("Employee ID : %d\n", emp_id);
printf("Employee Name : %s", emp_name);
fclose(fptr);
return 0;
}
Output :
Employee ID : 2 Employee Name : Raj
fgetc() - File Handling
fgetc() funtion से File में data को read किया जाता है |Syntax for fgetc()
fgetc(file_pointer);- file_pointer : जिस file को read करना है उस file का pointer |
Source Code :
#include <stdio.h>
int main(){
char ch;
FILE *fptr;
fptr = fopen("sample.txt","r");
while((!feof(fptr))){
ch = fgetc(fptr);
printf("%c",ch);
}
fclose(fptr);
return 0;
}
Output :
2, Raj
fputc() - File Handling
fputc() funtion से File में data को write किया जाता है |Syntax for fputc()
fputc(character, file_pointer);- character : जो character file में write करना है वो character यहाँ पे आता है |
- file_pointer : जिस file को write करना है उस file का pointer |
#include <stdio.h>
int main(){
char ch;
FILE *fptr;
fptr = fopen("sample.txt","w");
fputc('h',fptr);
fclose(fptr);
return 0;
}
Output :
h
fseek() - File Handling
fseek() funtion से दिए हुई position पर file pointer को set किया जाता है |Syntax for fseek()
fseek(file_pointer, offset, position);- file_pointer : जिस file को read और write करना है उस file का pointer |
- offset : File के data को beginning(SEEK_SET) से, end(SEEK_END) से और current(SEEK_CUR) position से कितने characters या bytes replace या store करना है वो integer value यहाँ पर आती है |
- position : File के data को कहा से replace करे या store करे ये वो position है |
for eg. SEEK_SET(0), SEEK_CUR(1), SEEK_END(2)
Hello WorldSource Code :
#include <stdio.h>
int main(){
FILE *fptr;
char str[20];
fptr = fopen("sample.txt","r+");
fgets(str, 20, fptr);
printf("Old file contents : %s\n", str); // Old contents : Hello World
fseek(fptr, 6, SEEK_SET); // replace contents after 6 characters
fputs("Friends", fptr); // put Friends after 6 characters
fclose(fptr); //file is closed
fptr = fopen("sample.txt","r"); // file is reopened
// User can use rewind() function instead of reopening file
fgets(str, 20, fptr);
printf("New file contents : %s\n", str);
fclose(fptr);
return 0;
}
Output :
Old file contents : Hello World New file contents : Hello Friends
getw() and putw() - File Handling
putw()
putw() funtion से Integer value को file में write किया जाता है |Syntax for putw()
putw(integer, file_pointer);- integer : जो integer value या उसका variable file में write करना है वो यहाँ पर आता है |
- file_pointer : जिस file में integer value को write करना है वो file pointer यहाँ पर आता है |
getw()
getw() funtion से Integer value को file में से read किया जाता है |Syntax for getw()
getw(file_pointer);- file_pointer : जिस file का integer read करना है उस file का pointer |
Example for putw() and getw()
Source Code :
#include <stdio.h>
int main()
{
FILE *fptr;
int num = 5;
fptr=fopen("file.txt","w");
putw(num,fptr);
printf("put value : %d\n",num);
fclose(fptr);
fptr=fopen("file.txt","r"); // file is reopened
int num1 = getw(fptr);
printf("get value : %d\n",num1);
fclose(fptr);
return 0;
}
Output :
put value : 5 get value : 5
ftell() - File Handling
ftell() funtion File Pointer की current position return करता है |Syntax for ftell()
ftell(file_pointer);- file pointer : ये एक file pointer है | जिससे file की current position समझ आएगी |
Source Code :
#include <stdio.h>
int main(){
FILE *fptr;
char str[] = "Hello World";
fptr=fopen("file.txt","w");
fputs(str,fptr);
printf("File Contents : %s\n",str);
int size = ftell(fptr);
printf("Size of file in bytes : %d\n", size);
fclose(fptr);
return 0;
}
Output :
File Contents : Hello World Size of file in bytes : 11
rewind() - File Handling
rewind() funtion से file pointer को file के शुरुआत में लाता है |Syntax for rewind()
rewind(file_pointer);- file pointer : जिस file को इस file pointer में open किया है, उस file pointer को शुरुआत में लाता है |
#include <stdio.h>
int main(){
FILE *fptr;
char str[20];
fptr = fopen("file.txt","r+");
fgets(str, 20, fptr);
printf("Old file contents : %s\n", str);
fseek(fptr, 6, SEEK_SET);
fputs("Friends", fptr);
rewind(fptr); // file is reopened
fgets(str, 20, fptr);
printf("New file contents : %s\n", str);
fclose(fptr);
return 0;
}
Output :
Old File Contents : Hello World New file contents : Hello Friends
remove() - File Handling
remove() funtion से file को delete या remove किया जाता है |Syntax for remove()
remove(file_name);- file_name : जिस file को remove करना उस file का नाम यहाँ पर आएगा |
Source Code :
#include <stdio.h>
int main(){
int remover;
char file[20];
printf("Enter file name to delete : ");
scanf("%s", file);
remover = remove(file);
if(remover == 0){
printf("File is removed.");
}else{
printf("Unable to delete file.");
}
return 0;
}
Output :
Enter file name to delete : hello.c Unable to delete file.







No comments: