Formatted I/O Functions in C

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

FunctionUse
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

  1. file "data.txt" write mode में open की गई
  2. variables define किए गए (id, salary)
  3. fprintf() से formatted data file में लिखा गया
fprintf(fp, "ID = %d\n", id);

यह बिल्कुल printf() जैसा है, लेकिन output file में जाता है

  1. 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

  1. file "data.txt" read mode में open की गई
  2. fscanf() का use करके file से data read किया
fscanf(fp, "ID = %d\n", &id);

format string match होना जरूरी है

  1. values variables में store हुई
  2. 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()

Featurefprintf()fscanf()
WorkWriteRead
Similar toprintf()scanf()
DirectionFile में data जाता हैFile से data आता है
FormatRequiredRequired

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) में बहुत उपयोगी होते हैं।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top