- 1 1. fprintf function भनेको
- 2 2. fprintf को आधारभूत प्रयोग
- 3 3. fprintf मा ढाँचा निर्दिष्टीकरणको उपयोग
- 4 4. फाइल अपरेसन र fprintf
- 5 5. त्रुटि ह्यान्डलिङ
- 6 6. अनुप्रयोग उदाहरण
- 7 7. बारम्बार सोधिने प्रश्नहरू (FAQ)
- 8 8. बहु फाइलको एकै साथ आउटपुट
- 9 9. सन्दर्भ लिंक
1. fprintf function भनेको
fprintf को मूल सारांश
fprintf
function C भाषा मा प्रयोग हुने मानक इनपुट/आउटपुट function मध्ये एक हो। यस function को मुख्य भूमिका “फरम्याट सहित स्ट्रिङ्ग आउटपुट गर्ने” हो। fprintf
प्रयोग गरेर, निर्दिष्ट फरम्याट अनुसार डेटा स्वरूपित गरी आउटपुट गन्तव्यमा लेख्न सकिन्छ। सामान्यतया、fprintf
निम्न परिस्थितिहरूमा प्रयोग गरिन्छ।
- Log File Creation
- Saving Formatted Data
- Output of Debug Information
fprintf को मूल संरचना
- Log File Creation
- Saving Formatted Data
- Output of Debug Information
int fprintf(FILE *stream, const char *format, ...);
संरचनाका प्रत्येक भाग
FILE *stream
stdout
fopen
const char *format
printf
...
रिटर्न मान सफलतापूर्वक लेखिएको क्यारेक्टर संख्या(धनात्मक पूर्णांक)हो। त्रुटि भएमा-1
फिर्ता गरिन्छ।
अन्य function हरूसँग तुलना
fprintf
सँग समान function हरूमा printf
र sprintf
छन्। प्रत्येकको भिन्नता तल सारांशित गरिएको छ।
printf सँगको भिन्नता
printf
मानक आउटपुट(सामान्यतया कन्सोल)मा डेटा आउटपुट गर्न प्रयोग हुन्छ। अर्कोतर्फ, fprintf
आउटपुट गन्तव्य निर्दिष्ट गर्न सकिन्छ, जसले लचिलोपन प्रदान गर्छ।उदाहरण: printf को प्रयोग
printf("Hello, World!n");
यो सधैं कन्सोलमा आउटपुट हुन्छ।उदाहरण: fprintf को प्रयोग
FILE *file = fopen("output.txt", "w");
fprintf(file, "Hello, World!n");
fclose(file);
यस अवस्थामा, आउटपुट निर्दिष्ट फाइल(output.txt
)मा लेखिन्छ।
sprintf सँगको भिन्नता
sprintf
को आउटपुट गन्तव्य “स्ट्रिङ” हो, जसले भिन्नता ल्याउँछ। अर्थात्, लेख्ने स्थान मेमोरी भित्रको बफर हुन्छ।उदाहरण: sprintf को प्रयोग
char buffer[50];
sprintf(buffer, "The result is %d", 42);
यस अवस्थामा, स्ट्रिङ@\”The result is 42\” bufferbuffer
मा लेखिन्छ।
सारांश
fprintf
- By using it appropriately with other output functions ( and ), it is possible to improve the efficiency and readability of the program.
2. fprintf को आधारभूत प्रयोग
सिन्ट्याक्स र आधारभूत तर्कहरूको व्याख्या
fprintf
कार्यले डेटा लाई ढाँचा सहित आउटपुट गर्नको लागि लचिलो उपकरण हो। यसको आधारभूत संरचना तल देखाइन्छ。
int fprintf(FILE *stream, const char *format, ...);
तल तर्कहरूको विस्तृत विवरण दिइएको छ।
- FILE *stream
- Specifies the write destination.
- Common options:
- const char *format
- Defines the output format.
- You can specify formats for strings, integers, floating-point numbers, etc., using format specifiers (e.g., , , ).
- परिवर्तनीय लम्बाइका तर्कहरू (…)”
- Provides data corresponding to the format specifier.
- Example: If the format is , pass the name and age as the corresponding data.
रिटर्न मानको रूपमा, सफलतापूर्वक लेखिएका क्यारेक्टरहरूको संख्या (धनात्मक पूर्णांक) फिर्ता गरिन्छ। त्रुटि भएमा -1
फिर्ता गरिन्छ।
आधारभूत कोड उदाहरण
तल fprintf
प्रयोग गरेर बनाइएको सरल उदाहरण देखाइन्छ।
मानक आउटपुटमा आउटपुट
मानक आउटपुट (stdout
) मा ढाँचा सहित स्ट्रिङ्ग आउटपुट गर्दछ。
#include
int main() {
fprintf(stdout, "Hello, %s! You have %d new messages.n", "Alice", 5);
return 0;
}
आउटपुट परिणाम:
Hello, Alice! You have 5 new messages.
यस उदाहरणमा, मानक आउटपुटको रूपमा stdout
लाई स्पष्ट रूपमा निर्दिष्ट गरिएको छ।
फाइलमा आउटपुट
फाइलमा डेटा लेख्दा fprintf
प्रयोग गरिन्छ。
#include
int main() {
FILE *file = fopen("output.txt", "w"); // फाइललाई लेख्ने मोडमा खोल्नुहोस्
if (file == NULL) {
fprintf(stderr, "Error opening file.n");
return 1;
}
fprintf(file, "Name: %s, Age: %dn", "Bob", 30);
fclose(file); // फाइल बन्द गर्नुहोस्
return 0;
}
output.txt को सामग्री:
Name: Bob, Age: 30
ढाँचा निर्दिष्टकर्ता को आधारभूत
fprintf
मा, ढाँचा निर्दिष्टकर्ता प्रयोग गरेर आउटपुटको स्वरूपलाई लचिलो रूपमा नियन्त्रण गर्न सकिन्छ। तल आधारभूत निर्दिष्टकर्ताहरूको उदाहरणहरू छन्。
specifier | Explanation | Example |
---|---|---|
%d | decimal integer | 42 |
%f | floating-point number | 3.141593 |
%s | string | "Hello" |
%c | single character | 'A' |
%x | hexadecimal (lowercase) | 0x2a |
%o | octal | 052 |
उदाहरण:
fprintf(stdout, "Integer: %d, Float: %.2f, String: %sn", 10, 3.14, "Test");
आउटपुट परिणाम:
Integer: 10, Float: 3.14, String: Test
3. fprintf मा ढाँचा निर्दिष्टीकरणको उपयोग
चौडाइ (Minimum Width)
यदि चौडाइ निर्दिष्ट गरियो भने, आउटपुट अक्षरसंख्या निर्दिष्ट गरिएको चौडाइभन्दा कम भएमा स्पेसले भरिन्छ।
उदाहरण:
fprintf(stdout, "|%10s|n", "Hello");
fprintf(stdout, "|%10d|n", 123);
आउटपुट परिणाम:
| Hello|
| 123|
यहाँ, चौडाइलाई 10 मा निर्दिष्ट गरिएको छ। अक्षरसंख्या कम भएमा, बायाँपट्टि स्पेस घुसाइन्छ।
सटीकता (Precision)
सटीकता, तलका प्रयोग अनुसार विभिन्न अर्थहरू राख्दछ।
- String (%s)
- Floating-point numbers (%f, %e, %g)
उदाहरण:
fprintf(stdout, "%.3fn", 3.141592); // फ्लोटिङ्ग प्वाइन्ट सङ्ख्याको शुद्धता
fprintf(stdout, "%.5sn", "Hello, World!"); // स्ट्रिङको अधिकतम लम्बाइ
आउटपुट परिणाम:
3.142
Hello
फ्ल्याग
फ्ल्याग प्रयोग गरेर, आउटपुटको व्यवस्था र ढाँचा नियन्त्रण गर्न सकिन्छ।
flag | Explanation | I’m ready to translate the snippet you have in mind. Could you please provide the text you’d like me to translate? |
---|---|---|
- | Left-aligned (default is right-aligned) | |%-10s| → |Hello | |
+ | Always display the sign of the number (even for positive numbers, display ) | %+d → +42 |
0 | Zero padding (effective when width is specified) | %05d → 00042 |
# | Format specification with a specific type (hexadecimal and octal) | %#x → 0x2a |
Insert a space at the beginning of a positive number | % d → 42 |
उदाहरण:
fprintf(stdout, "|%-10s|%+05d|%#x|n", "Left", 42, 42);
आउटपुट परिणाम:
|Left |+0042|0x2a|
व्यावहारिक प्रयोग उदाहरण
fprintf
को चौडाइ, सटीकता, फ्ल्यागलाई संयोजन गरेर, ढाँचा गरिएको तालिका स्वरूपको डेटा सिर्जना गर्न सकिन्छ।
तालिका स्वरूपको डेटा आउटपुट
तलमा, विद्यार्थीहरूको ग्रेडलाई ढाँचा सहित आउटपुट गर्ने उदाहरण छ।
#include
int main() {
fprintf(stdout, "|%-10s|%5s|%5s|%5s|n", "Name", "Math", "Eng", "Sci");
fprintf(stdout, "|%-10s|%5d|%5d|%5d|n", "Alice", 95, 88, 92);
fprintf(stdout, "|%-10s|%5d|%5d|%5d|n", "Bob", 82, 79, 85);
return 0;
}
आउटपुट परिणाम:
|Name | Math| Eng| Sci|
|Alice | 95| 88| 92|
|Bob | 82| 79| 85|
संख्यात्मक डेटा स्वरूपण
विशिष्ट संख्यालाई ढाँचा सहित आउटपुट गरेर, एकरूप देखावट बनाइन्छ।
उदाहरण:
fprintf(stdout, "Price: $%8.2fn", 1234.5);
fprintf(stdout, "Discount: %06d%%n", 25);
आउटपुट परिणाम:
Price: $ 1234.50
Discount: 000025%
ध्यान दिनु पर्ने बुँदाहरू
- अवैध फर्म्याट निर्दिष्टकर्ता
- If the format specifier and data type do not match, unexpected output or errors may occur.
- Example: Passing a string to may result in undefined behavior.
- चौडाइ र शुद्धताको निर्दिष्टीकरण
- Specifying an excessively large width may make the output redundant and waste resources.
सारांश
- By utilizing width, precision, and flags, the output of can be finely controlled.
- By effectively handling table-format data and numerical formatting, you can make the program’s output easier to read.
- By ensuring that the format specifier matches the data type being passed, safe output can be achieved.
4. फाइल अपरेसन र fprintf
फाइल खोल्ने तरिका(fopen)
fprintf
प्रयोग गरेर फाइलमा डेटा लेख्नको लागि, पहिलो पटक फाइल खोल्न आवश्यक छ। C भाषा मा, fopen
फङ्क्शन प्रयोग गरेर फाइल खोलिन्छ।
fopen को आधारभूत संरचना
FILE *fopen(const char *filename, const char *mode);
आर्ग्युमेन्टको व्याख्या
filename
mode
"r"
"w"
"a"
"rb"
"wb"
"ab"
r
w
a
फिर्ती मान
- If the file is opened successfully, it returns a type pointer.
- If it fails, is returned.
fopen को प्रयोग उदाहरण
#include
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
fprintf(file, "Hello, World!n");
fclose(file);
return 0;
}
यो प्रोग्रामले example.txt
नामको फाइल खोल्छ, सामग्री लेखेर बन्द गर्छ।
fprintf प्रयोग गरेर फाइल लेखन
fprintf
प्रयोग गरेर, खोलिएको फाइलमा फर्म्याट सहित डेटा लेख्न सकिन्छ। तल केही परिदृश्यहरूमा प्रयोगका उदाहरणहरू देखाइएका छन्।
आधारभूत फाइल लेखन
#include
int main() {
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
fprintf(file, "Name: %s, Age: %dn", "Alice", 25);
fprintf(file, "Name: %s, Age: %dn", "Bob", 30);
fclose(file);
return 0;
}
data.txt को सामग्री:
Name: Alice, Age: 25
Name: Bob, Age: 30
CSV फाइलको निर्माण
CSV(Comma-Separated Values)स्वरूपको डेटा लेख्ने उदाहरण हो।
#include
int main() {
FILE *file = fopen("students.csv", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
// हेडर पंक्ति
fprintf(file, "Name,Math,English,Sciencen");
// डेटा पंक्ति
fprintf(file, "Alice,95,88,92n");
fprintf(file, "Bob,82,79,85n");
fclose(file);
return 0;
}
students.csv को सामग्री:
Name,Math,English,Science
Alice,95,88,92
Bob,82,79,85
फाइल बन्द गर्ने(fclose)
फाइल अपरेसन समाप्त भएपछि, fclose
फङ्क्शन प्रयोग गरेर फाइल बन्द गर्न आवश्यक छ। यदि यो नगरेमा, तलका समस्याहरू उत्पन्न हुन सक्छन्।
- Data writing to the file is not fully performed.
- System resources are wasted.
fclose को आधारभूत संरचना
int fclose(FILE *stream);
फिर्ती मान
- If successful, .
- If it fails, it returns EOF (End of File).
fclose को उदाहरण
FILE *file = fopen("example.txt", "w");
if (file != NULL) {
fprintf(file, "This is a test.n");
fclose(file);
}
सुरक्षित फाइल अपरेसनका टिप्स
- फाइल पोइन्टरको जाँच
- Always check if the return value of is .
- फाइल बन्द गर्न नबिर्सनु रोकथाम
- When you open a file, always call .
- त्रुटि व्यवस्थापन
- Detect and handle errors during file operations.
- Example: Insufficient disk capacity or file permission error.
सारांश
fprintf
fopen
fclose
- By properly handling file operation modes and error handling, safe and efficient file operations become possible.
- As an application, it can be used for saving data in CSV format or for logging.
5. त्रुटि ह्यान्डलिङ
fprintf को रिटर्न मान प्रयोग गरेर त्रुटि प्रक्रिया
fprintf
को रिटर्न मान जाँच गरेर, लेखन अपरेसन सफल भयो कि भएन भन्ने निर्धारण गर्न सकिन्छ।
रिटर्न मानको विशिष्टता
- If successfully written: Return the number of characters written (positive integer).
- If an error occurs: return .
मूलभूत त्रुटि जाँच उदाहरण
#include
int main() {
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
int result = fprintf(file, "Hello, World!n");
if (result < 0) {
fprintf(stderr, "Error: Failed to write to file.n");
}
fclose(file);
return 0;
}
यो प्रोग्राममा, fprintf
को रिटर्न मान जाँच गरी, लेखन असफल भएमा त्रुटि सन्देश देखाइन्छ।
मानक त्रुटि आउटपुट (stderr) को उपयोग
stderr
प्रोग्रामले त्रुटि वा चेतावनी रिपोर्ट गर्न प्रयोग गर्ने मानक आउटपुट स्ट्रिम हो।stderr
मा त्रुटि सन्देश देखाएर, प्रयोगकर्ता वा विकासकर्तालाई समस्या स्पष्ट रूपमा बताउन सकिन्छ।
उदाहरण: stderr प्रयोग गरेर त्रुटि आउटपुट
#include
int main() {
FILE *file = fopen("nonexistent_directory/output.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Unable to open file. Check the directory path.n");
return 1;
}
fclose(file);
return 0;
}
आउटपुट परिणाम (त्रुटि हुँदा):
Error: Unable to open file. Check the directory path.
stderr
प्रयोग गरेर, त्रुटि आउटपुटलाई सामान्य मानक आउटपुट (stdout
) बाट अलग गर्न सकिन्छ।
व्यावहारिक त्रुटि प्रक्रिया कोड
तलको उदाहरण फाइल अपरेसनमा हुने सामान्य त्रुटिहरूलाई ह्यान्डल गर्ने व्यावहारिक उदाहरण हो।
उदाहरण: लेखन र फाइल बन्द गर्ने त्रुटि प्रक्रिया
#include
int main() {
FILE *file = fopen("output.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
// लेखन प्रक्रिया
if (fprintf(file, "Logging data: %dn", 42) < 0) {
fprintf(stderr, "Error: Failed to write to file.n");
fclose(file);
return 1;
}
// फाइल बन्द गर्दा त्रुटि जाँच
if (fclose(file) != 0) {
fprintf(stderr, "Error: Failed to close the file.n");
1;
}
printf("File operation completed successfully.n");
return 0;
}
बुँदा:
- फाइल खोल्ने, लेख्ने, बन्द गर्ने प्रत्येक चरणमा त्रुटि जाँच गरिरहेछौं।
- त्रुटि हुँदा उपयुक्त सन्देश प्रिन्ट गरेर कार्यक्रम समाप्त गरिन्छ।
सामान्य त्रुटिहरू र समाधान
1. फाइल खोल्न सकिएन
कारण:
- The file does not exist.
- The directory is wrong.
- Insufficient access permissions.
समाधान:
- Check the file path.
- Fix the access permissions.
- Check the return value of .
2. लेखन असफल हुन्छ
कार:
- Insufficient disk space.
- The file is open as read-only.
समाधान:
- Check the file mode ( or ).
- Check the disk capacity.
3. फाइल बन्द गर्दा त्रुटि
कारण:
- System resources are insufficient.
- Hardware malfunction.
समाधान:
- Check for errors when closing.
- When opening a file, use the minimum necessary resources.
सारांश
fprintf
- By using standard error output (), error messages can be reported appropriately.
- By properly implementing error handling for general file operations, the program’s reliability improves.
6. अनुप्रयोग उदाहरण
लग फाइलको स्वचालित निर्माण
लग फाइललाई कार्यक्रमको कार्यस्थिति र त्रुटि जानकारी रेकर्ड गर्न प्रयोग गरिन्छ। तल मिति र समय समावेश गर्ने लग रेकर्ड गर्ने उदाहरण छ।
उदाहरण: मिति‑समय सहितको लगको आउटपुट
#include
#include
int main() {
FILE *logFile = fopen("log.txt", "a"); // Append मोडमा खोल्नुहोस्
if (logFile == NULL) {
fprintf(stderr, "Error: Could not open log file.n");
return 1;
}
time_t now = time(NULL);
struct tm *localTime = localtime(&now);
fprintf(logFile, "[%04d-%02d-%02d %02d:%02d:%02d] Program startedn",
localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,
localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
fclose(logFile);
return 0;
}
लग फाइलको सामग्री:
[2025-01-19 15:45:30] Program started
मुख्य बिन्दु
time.h
- Using append mode (), it appends the log to the end of the file.
टेबल स्वरूप डेटा लेखन
डेटालाई तालिका स्वरूपमा व्यवस्थित रूपमा आउटपुट गर्ने उदाहरण देखाइन्छ। यो परिणाम रिपोर्ट वा डाटाबेस जानकारी निर्यात गर्दा उपयोगी हुन्छ।
उदाहरण: विद्यार्थीको ग्रेड तालिका आउटपुट
#include
int main() {
FILE *file = fopen("report.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
fprintf(file, "|%-10s|%6s|%6s|%6s|n", "Name", "Math", "Eng", "Sci");
fprintf(file, "|%-10s|%6d|%6d|%6d|n", "Alice", 90, 85, 88);
fprintf(file, "|%-10s|%6d|%6d|%6d|n", "Bob", 78, 82, 80);
fclose(file);
return 0;
}
report.txt को सामग्री:
|Name | Math| Eng| Sci|
|Alice | 90| 85| 88|
|Bob | 78| 82| 80|
मुख्य बिन्दु
- Using left alignment () or right alignment (), we have achieved a readable format.
CSV फाइलमा डेटा सुरक्षित गर्नु
CSV (Comma-Separated Values) डेटा सुरक्षित गर्न र अन्य कार्यक्रमहरूसँग डेटा साटासाट गर्न उपयोगी हुन्छ।
उदाहरण: डेटा CSV स्वरूपमा सुरक्षित गर्नु
#include
int main() {
FILE *file = fopen("data.csv", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
// हेडर पंक्ति रेकर्ड गर्नुहोस्
fprintf(file, "Name,Math,English,Sciencen");
// डेटा पंक्ति रेकर्ड गर्नुहोस्
fprintf(file, "Alice,90,85,88n");
fprintf(file, "Bob,78,82,80n");
fclose(file);
return 0;
}
data.csv को सामग्री:
Name,Math,English,Science
Alice,90,85,88
Bob,78,82,80
मुख्य बिन्दु
- By separating each field with commas (), it creates a format that can be read by other tools (Excel or Python).
डिबग जानकारीको रेकर्ड
डिबगको लागि लग रेकर्ड गरेर, कार्यक्रमको स्थितिलाई सजिलै ट्र्याक गर्न सकिन्छ।
उदाहरण: चलाउदा चलको रेकर्ड
#include
int main() {
FILE *debugFile = fopen("debug.log", "w");
if (debugFile == NULL) {
fprintf(stderr, "Error: Could not open debug log file.n");
return 1;
}
int x = 42;
fprintf(debugFile, "Debug: Variable x = %dn", x);
fclose(debugFile);
return 0;
}
debug.log को सामग्री:
Debug: Variable x = 42
मुख्य बिन्दु
- By logging debug information to a file, it becomes easier to identify issues in complex programs.
सारांश
fprintf
- In practice, logs with timestamps or CSV format are particularly useful.
- Through application examples, you will be able to utilize more effectively.

7. बारम्बार सोधिने प्रश्नहरू (FAQ)
1. fprintf र printf बीचको फरक के हो?
उत्तर
printf
:- Outputs data to standard output (usually the console).
- You cannot change the output destination.
fprintf
:- You can freely specify the output destination (e.g., file, standard output, standard error output, etc.).
- More flexible data output is possible.
उदाहरण
#include
int main() {
printf("This is printed to the console.n"); // सधैँ मानक आउटपुट
FILE *file = fopen("output.txt", "w");
if (file != NULL) {
fprintf(file, "This is written to a file.n"); // फाइलमा आउटपुट
fclose(file);
}
return 0;
}
2. fprintf मा जापानी भाषा सही रूपमा आउटपुट गर्न कसरी?
उत्तर
- To correctly output Japanese, you need to confirm the following points.
- अक्षर एन्कोडिङ:
- उपयोग गर्ने वातावरण अनुसार, उपयुक्त क्यारेक्टर एन्कोडिङ (उदाहरण: UTF-8, Shift-JIS) सेट गर्नुहोस्।
- फाइलको एन्कोडिङ:
- लेखन गरिने फाइलको एन्कोडिङलाई क्यारेक्टर कोडसँग मिलाएर समायोजन गर्नुहोस्।
उदाहरण: UTF-8 मा जापानी भाषा आउटपुट
#include
#include
int main() {
setlocale(LC_ALL, ""); // लोकेल सेट गर्नुहोस्
FILE *file = fopen("japanese.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
fprintf(file, "नमस्ते, संसार!n");
fclose(file);
return 0;
}
ध्यान:
- Depending on the environment, it may be necessary to explicitly set the encoding to prevent garbled text (e.g., using Shift-JIS on Windows).
3. fprintf मा त्रुटि उत्पन्न हुने मुख्य कारण के हो?
उत्तर
- The main causes of errors include the following.
- फाइल खोल्न सकिँदैन:
- फाइल पथ गलत छ।
- पहुँच अधिकारहरू पर्याप्त छैनन्।
- डिस्क स्थान अपुरै:
- लेखन गर्दा खाली स्थान पर्याप्त हुँदैन।
- फर्म्याट निर्दिष्टकर्ता असंगति:
- डेटा प्रकार र फर्म्याट निर्दिष्टकर्ता मेल खाँदैन।
उदाहरण: फर्म्याट स्पेसिफायरको असमानता
#include
int main() {
FILE *file = fopen("error.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
// फोर्म्याट स्पेसिफायरले स्ट्रिङको अपेक्षा गर्दछ, तर पूर्णांक पठाइरहेको छ
fprintf(file, "%s", 42); // त्रुटि उत्पन्न
fclose(file);
return 0;
}
उपाय:
- Please confirm the format specifier and the data type to pass (example: is an integer, is a string).
4. fprintf मा बफरिङको प्रभाव के हो?
उत्तर
- Buffering
- Output is temporarily stored in a buffer, and is not written to the file until the buffer becomes full or or is called.
- Issues
- If the program terminates abnormally, the data accumulated in the buffer may be lost without being written to the file.
उपाय
fflush
उपयोग गर्नु:
- By manually flushing the buffer, the data is written immediately.
- उदाहरण:
fflush
को प्रयोग
#include
int main() {
FILE *file = fopen("buffered_output.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
fprintf(file, "Buffered data.n");
fflush(file); // बफरलाई फ्लश गरेर तुरुन्तै लेख्नुहोस्
fclose(file);
return 0;
}
5. फाइल आउटपुट बीचमा टुटेमा कसरी समाधान गर्ने?
उत्तर
- The following can be considered as causes for the file output being interrupted midway.
- फाइल बन्द गरिएको छैन:
- बफर फ्लश गरिँदैन, र लेखिन नसकेको डेटा हराउँछ।
- डिस्क स्थान अपुरै:
- लेखन गर्दा खाली स्थान पर्याप्त हुँदैन।
उदाहरण: फाइललाई सही रूपमा बन्द गर्ने
#include
int main() {
FILE *file = fopen("partial_output.txt", "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file.n");
return 1;
}
fprintf(file, "This is complete data.n");
// fclose गर्न नबिर्सनै
fclose(file);
return 0;
}
उपाय:
fclose
- If an error occurs during writing, check the return value.
सारांश
fprintf
- By referring to the content covered in the FAQ, you can prevent common troubles in advance.
8. बहु फाइलको एकै साथ आउटपुट
fprintf
प्रयोग गर्दा, बहु फाइलहरूमा एकै साथ डेटा लेख्न सकिन्छ। यस खण्डमा, व्यावहारिक रूपमा उपयोगी एकै साथ आउटपुटको विधिहरूलाई व्याख्या गर्छौं।
एकै साथ बहु फाइलहरूलाई ह्यान्डल गर्ने आधारभूत संरचना
C gengo ma, bahulā FILE
pointer harū prayog garera, bahulā fā’ila harūlai ekai samaya ma sanchālan garna sakinchha. Pratyek file pointer ko lagi fopen
, fprintf
, fclose
लाई उपयुक्त रूपमा कार्यान्वयन गर्नु महत्त्वपूर्ण छ।
आधारभूत उदाहरण: 2 फाइलहरूमा एकै साथ आउटपुट
#include
int main() {
// दुई फाइल खोल्नुहोस्
FILE *file1 = fopen("output1.txt", "w");
FILE *file2 = fopen("output2.txt", "w");
if (file1 == NULL || file2 == NULL) {
fprintf(stderr, "Error: Could not open one of the files.n");
if (file1) fclose(file1);
if (file2) fclose(file2);
return 1;
}
// फाइल1 मा डेटा लेख्नुहोस्
fprintf(file1, "This is the first file.n");
// फाइल2 मा डेटा लेख्नुहोस्
fprintf(file2, "This is the second file.n");
// फाइलहरू बन्द गर्नुहोस्
fclose(file1);
fclose(file2);
printf("Data written to both files successfully.n");
return 0;
}
output1.txt को सामग्री:
This is the first file.
output2.txt को सामग्री:
This is the second file.
बुँदा
- त्रुटि जाँच:
- Check if each succeeded.
- संसाधन विमोचन:
- Be sure to close all opened files with .
डायनामिक फाइल अपरेसन
डायनामिक रूपमा फाइलनाम सिर्जना गरी, बहु फाइलहरूमा डेटा लेख्ने उदाहरण प्रस्तुत गर्दछौं।
उदाहरण: डायनामिक फाइलनाम प्रयोग गरेर आउटपुट
#include
int main() {
char filename[20];
for (int i = 1; i <= 3; i++) {
// फाइल नाम गतिशील रूपमा सिर्जना गर्नुहोस्
sprintf(filename, "file%d.txt", i);
// फाइल खोल्नुहोस्
FILE *file = fopen(filename, "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open %sn", filename);
continue; // अर्को फाइलमा जानुहोस्
}
// फाइलमा लेख्नुहोस्
fprintf(file, "This is file number %dn", i);
// फाइल बन्द गर्नुहोस्
fclose(file);
}
printf("Data written to files successfully.n");
return 0;
}
उत्पन्न हुने फाइलहरूको उदाहरण:
file1.txt
:This is file number 1
file2.txt
:This is file number 2
file3.txt
:This is file number 3
बुँदा
- Dynamically generate file names using .
- If an error occurs, proceed to the next loop.
बहु फाइलहरूमा समानान्तर लेखन
बहु फाइलहरूमा एकै साथ ठूलो मात्रामा डेटा लेख्ने अवस्थामा, समानान्तर प्रक्रिया (थ्रेड) प्रयोग गर्न सकिन्छ।
उदाहरण: थ्रेड प्रयोग गरेर समानान्तर लेखन
निम्नकोड POSIX थ्रेड (pthread) प्रयोग गरेर समानान्तर लेखनको उदाहरण हो।
#include
#include
void *write_to_file(void *arg) {
char *filename = (char *)arg;
FILE *file = fopen(filename, "w");
if (file == NULL) {
fprintf(stderr, "Error: Could not open %sn", filename);
return NULL;
}
fprintf(file, "Data written to %sn", filename);
fclose(file);
return NULL;
}
int main() {
pthread_t threads[3];
char *filenames[] = {"thread1.txt", "thread2.txt", "thread3.txt"};
for (int i = 0; i < 3; i++) {
pthread_create(&threads[i], NULL, write_to_file, filenames[i]);
}
for (int i = 0; i < 3; i++) {
pthread_join(threads[i], NULL);
}
printf("Data written to all files in parallel.n");
return 0;
}
उत्पन्न हुने फाइलहरूको उदाहरण:
thread1.txt
:Data written to thread1.txt
thread2.txt
:Data written to thread2.txt
thread3.txt
:Data written to thread3.txt
बुँदा
- By using threads, it is possible to write to multiple files in parallel.
- Do not forget thread synchronization ().
सारांश
fprintf
- By utilizing threads for dynamic file name generation, flexibility and performance improve.
- Resource management (रerror handling) can be thoroughly implemented to achieve a safe program.
9. सन्दर्भ लिंक