C के fprintf() और fscanf()
In this page:
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()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
fscanf() से Formatted Text पढ़ना
fscanf() scanf() जैसे ही %d, %s, %f format specifiers इस्तेमाल करता है, और scanf() की तरह, इसे non-string variables के लिए addresses चाहिए (& इस्तेमाल करते हुए) यह जानने के लिए कि जो पढ़े उसे कहाँ store करना है।
उदाहरण: Reading formatted text with fscanf()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
fprintf() से Data Append करना
क्योंकि a mode किसी भी write से पहले file pointer को अंत में position करता है, कई program runs में repeated fprintf() calls हर बार पिछली entries overwrite करने की बजाय एक बढ़ती log file बनाते हैं।
उदाहरण: Appending data with fprintf()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
fscanf() में EOF Handle करना
fscanf() उन items की संख्या return करता है जिन्हें उसने successfully match और assign किया, और एक बार पढ़ने को कोई data न बचे तो यह आपकी expected count से नीचे गिर जाता है (या पूरी तरह EOF hit करता है) -- यह check करना variable-length input का अंत detect करने का तरीका है।
उदाहरण: Handling EOF in fscanf()
// 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;
}
Login to try C/C++/Java/PHP code in the editor
fscanfमें एक ऐसा format specifier इस्तेमाल करना जो variable type से match न करे, जैसे एकfloatके लिए%d।fscanfका return value check न करना, ताकि एक failed read unnoticed रहे।- file बंद करना भूल जाना, ताकि buffered output खो सकता हो।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: