Introduction
C language में file pointer का उपयोग करके हम files में data पढ़ (input) और लिख (output) सकते हैं।
Keyboard से input लेना और screen पर output दिखाना अलग है
File I/O में data file से आता है और file में ही store होता है
File pointer (FILE *fp) के माध्यम से हम file को control करते हैं और I/O operations perform करते हैं।
File I/O क्या होता है
- Input (Reading) → file से data पढ़ना
- Output (Writing) → file में data लिखना
इसके लिए हम functions जैसे:
fprintf()→ file में write करने के लिएfscanf()→ file से read करने के लिए
use करते हैं।
1. Writing to File using File Pointer
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
fprintf(fp, "Name: Rahul\n");
fprintf(fp, "Age: 20\n");
fclose(fp);
printf("Data written successfully\n");
return 0;
}
Program Explanation
Step-by-step:
FILE *fp;→ file pointer declare कियाfopen("data.txt", "w");- file write mode में open हुई
- नई file create होगी
fprintf()का use
fprintf(fp, "Name: Rahul\n");
यह data file में लिखता है (screen पर नहीं)
fclose(fp);→ file close
File Content (data.txt):
Name: Rahul
Age: 20
Output:
Data written successfully
2. Reading from File using File Pointer
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
char name[20];
int age;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 1;
}
fscanf(fp, "Name: %s\n", name);
fscanf(fp, "Age: %d", &age);
printf("Name = %s\n", name);
printf("Age = %d\n", age);
fclose(fp);
return 0;
}
Program Explanation
- file
"data.txt"read mode में open की गई fscanf()का use करके file से data read किया
fscanf(fp, "Name: %s\n", name);
file से data लेकर variable में store किया गया
printf()से output screen पर दिखाया गया
Output:
Name = Rahul
Age = 20
Combined Example: Write + Read
#include <stdio.h>
int main() {
FILE *fp;
char data[50];
// Write
fp = fopen("file.txt", "w");
fprintf(fp, "Hello C File Handling");
fclose(fp);
// Read
fp = fopen("file.txt", "r");
fscanf(fp, "%[^\n]", data);
fclose(fp);
printf("Data from file: %s\n", data);
return 0;
}
Program Explanation
- पहले file में data लिखा गया
- फिर उसी file को read mode में open किया गया
fscanf()से पूरा line पढ़ा गया- output screen पर print हुआ
Output:
Data from file: Hello C File Handling
Important Functions for File I/O
| Function | Use |
|---|---|
fprintf() | File में formatted data लिखना |
fscanf() | File से formatted data पढ़ना |
Important Points
- File pointer जरूरी है (
FILE *fp) fopen()से file open करनी होती हैfprintf()→ writefscanf()→ readfclose()→ file close करना जरूरी
Common Mistakes
file open check (fp == NULL) नहीं करना
file close करना भूल जाना
गलत format use करना
Exam / Practical Tips
- Write + Read combined program बहुत पूछा जाता है
fprintf()औरfscanf()याद रखो- file modes (
r,w) clear होने चाहिए
यह topic practical exam में बहुत important है
Conclusion
File pointer के माध्यम से input/output करना C file handling का core concept है।
fprintf() और fscanf() functions की मदद से हम file में data store और retrieve कर सकते हैं।
यह concept real-world applications (data storage, logs, records) में बहुत उपयोगी है।