← Back to C Course | Chapter 10: File I/O | Lesson 4 of 6

C के fprintf() और fscanf()

fprintf एक file में neatly formatted text लिखता है और fscanf इसे वापस पढ़ता है, printf और scanf जैसा लेकिन screen की बजाय एक notebook के लिए। ये save किए values को load किए वालों से match करते हैं।
Syntax
c
fprintf(file_pointer, "format", values);

fscanf(file_pointer, "%specifier", &variable_name);

fprintf() से Formatted Text लिखना

इसका मतलब है printf() के साथ इस्तेमाल होने वाले सारे same format specifiers (%d, %s, %f, वगैरह) fprintf() के साथ identically काम करते हैं, बस console की बजाय एक file stream की ओर directed -- human-readable log files या reports बनाने के लिए useful।

उदाहरण: Writing formatted text with fprintf()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "Age: %d", 25);
	fclose(fp);
	// Print formatted text to the screen
	printf("Formatted text written to file");
	// Return 0 to signal that the program finished successfully
	return 0;
}

fscanf() से Formatted Text पढ़ना

fscanf() scanf() जैसे ही %d, %s, %f format specifiers इस्तेमाल करता है, और scanf() की तरह, इसे non-string variables के लिए addresses चाहिए (& इस्तेमाल करते हुए) यह जानने के लिए कि जो पढ़े उसे कहाँ store करना है।

उदाहरण: Reading formatted text with fscanf()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "%d", 42);
	fclose(fp);
	int value;
	fp = fopen("data.txt", "r");
	fscanf(fp, "%d", &value);
	fclose(fp);
	// Print formatted text to the screen
	printf("%d", value);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Mixed Data Types के साथ काम करना

उदाहरण के लिए, आप fprintf(fp, "%s,%d,%.2f\n", name, age, gpa) से एक CSV-style line लिख सकते हैं और बाद में same format string structure इस्तेमाल करने वाली एक matching fscanf() call से इसे वापस parse कर सकते हैं।

उदाहरण: Working with mixed data types

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.csv", "w");
	fprintf(fp, "%s,%d,%.2f\n", "Alice", 20, 3.5);
	fclose(fp);
	char name[20];
	int age;
	float gpa;
	fp = fopen("data.csv", "r");
	fscanf(fp, "%[^,],%d,%f", name, &age, &gpa);
	fclose(fp);
	// Print formatted text to the screen
	printf("%s %d %.2f", name, age, gpa);
	// Return 0 to signal that the program finished successfully
	return 0;
}

fprintf() से Data Append करना

क्योंकि a mode किसी भी write से पहले file pointer को अंत में position करता है, कई program runs में repeated fprintf() calls हर बार पिछली entries overwrite करने की बजाय एक बढ़ती log file बनाते हैं।

उदाहरण: Appending data with fprintf()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("log.txt", "w");
	fprintf(fp, "First entry\n");
	fclose(fp);
	fp = fopen("log.txt", "a");
	fprintf(fp, "Second entry\n");
	fclose(fp);
	// Print formatted text to the screen
	printf("Log grows across runs");
	// Return 0 to signal that the program finished successfully
	return 0;
}

fscanf() में EOF Handle करना

fscanf() उन items की संख्या return करता है जिन्हें उसने successfully match और assign किया, और एक बार पढ़ने को कोई data न बचे तो यह आपकी expected count से नीचे गिर जाता है (या पूरी तरह EOF hit करता है) -- यह check करना variable-length input का अंत detect करने का तरीका है।

उदाहरण: Handling EOF in fscanf()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "5");
	fclose(fp);
	int value;
	fp = fopen("data.txt", "r");
	// Declare result and set it to fscanf(fp, "%d", &value)
	int result = fscanf(fp, "%d", &value);
	fclose(fp);
	// Print formatted text to the screen
	printf("%d", result == 1);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. fscanf में एक ऐसा format specifier इस्तेमाल करना जो variable type से match न करे, जैसे एक float के लिए %d।
  2. fscanf का return value check न करना, ताकि एक failed read unnoticed रहे।
  3. file बंद करना भूल जाना, ताकि buffered output खो सकता हो।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.