Introduction
C language में जब हमें file में formatted तरीके से data लिखना या पढ़ना होता है, तब हम Formatted I/O Functions का उपयोग करते हैं।
ये functions printf() और scanf() की तरह होते हैं, लेकिन file के साथ काम करते हैं
fprintf()→ file में formatted data लिखने के लिएfscanf()→ file से formatted data पढ़ने के लिए
Formatted I/O Functions
| Function | Use |
|---|---|
fprintf() | File में formatted output (write) |
fscanf() | File से formatted input (read) |
1. Writing using fprintf()
Syntax
fprintf(file_pointer, "format string", variables);
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
int id = 101;
float salary = 25000.50;
fprintf(fp, "ID = %d\n", id);
fprintf(fp, "Salary = %.2f\n", salary);
fclose(fp);
printf("Data written successfully\n");
return 0;
}
Program Explanation
- file
"data.txt"write mode में open की गई - variables define किए गए (
id,salary) fprintf()से formatted data file में लिखा गया
fprintf(fp, "ID = %d\n", id);
यह बिल्कुल printf() जैसा है, लेकिन output file में जाता है
- file close की गई
File Content (data.txt):
ID = 101
Salary = 25000.50
Output:
Data written successfully
2. Reading using fscanf()
Syntax
fscanf(file_pointer, "format string", &variables);
Example Program:
#include <stdio.h>
int main() {
FILE *fp;
int id;
float salary;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("File not found\n");
return 1;
}
fscanf(fp, "ID = %d\n", &id);
fscanf(fp, "Salary = %f", &salary);
printf("ID = %d\n", id);
printf("Salary = %.2f\n", salary);
fclose(fp);
return 0;
}
Program Explanation
- file
"data.txt"read mode में open की गई fscanf()का use करके file से data read किया
fscanf(fp, "ID = %d\n", &id);
format string match होना जरूरी है
- values variables में store हुई
- output screen पर print किया गया
Output:
ID = 101
Salary = 25000.50
3. Combined Program (Write + Read)
#include <stdio.h>
int main() {
FILE *fp;
int roll = 1;
float marks = 89.5;
// Write
fp = fopen("student.txt", "w");
fprintf(fp, "Roll = %d\nMarks = %.1f", roll, marks);
fclose(fp);
// Read
fp = fopen("student.txt", "r");
fscanf(fp, "Roll = %d\nMarks = %f", &roll, &marks);
fclose(fp);
printf("Roll = %d\nMarks = %.1f\n", roll, marks);
return 0;
}
Output:
Roll = 1
Marks = 89.5
Important Points
fprintf()→ file में formatted data लिखता हैfscanf()→ file से formatted data पढ़ता है- format string सही match होना जरूरी है
- variables के साथ
&लगाना जरूरी है (reading में)
Difference: fprintf() vs fscanf()
| Feature | fprintf() | fscanf() |
|---|---|---|
| Work | Write | Read |
| Similar to | printf() | scanf() |
| Direction | File में data जाता है | File से data आता है |
| Format | Required | Required |
Advantages
1. Structured Data Handling
Formatted data store करना आसान
2. Readable Output
Data human-readable होता है
3. Flexible
Multiple data types handle कर सकते हैं
Limitations
- format mismatch होने पर error हो सकता है
- parsing complex हो सकता है
Exam / Practical Tips
fprintf()औरfscanf()बहुत important हैं- format string ध्यान से लिखें
- write + read program जरूर practice करें
Conclusion
Formatted I/O functions (fprintf() और fscanf()) file handling को powerful और structured बनाते हैं।
इनकी मदद से हम formatted data को आसानी से file में store और retrieve कर सकते हैं।
यह real-world applications (records, reports, logs) में बहुत उपयोगी होते हैं।