C File Handling परिचय
In this page:
FILE *file_pointer = fopen("filename.txt", "mode");
// use fprintf, fscanf, fgets, ...
fclose(file_pointer);
Files का Introduction
यही persistence है जिस वजह से programs restart से बचने वाली किसी भी चीज़ के लिए files इस्तेमाल करते हैं -- configuration settings, saved game progress, या logs सब इस तरह store होते हैं, सिर्फ program के in-memory variables में रहने की बजाय।
उदाहरण: Introduction to Files
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("notes.txt", "w");
fprintf(fp, "Saved data");
fclose(fp);
// Print formatted text to the screen
printf("Data persisted to a file");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
C में File Streams
FILE pointer (fopen() से return) एक opaque handle है -- आपको कभी इसकी internal structure जानने की ज़रूरत नहीं, सिर्फ इसे हर file function (fread, fwrite, fclose, वगैरह) को consistently pass करना है जो उस particular open file पर operate करता है।
उदाहरण: File Streams in 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, "Hello");
fclose(fp);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Standard Streams बनाम Files
stdin, stdout, और stderr असल में पहले से open FILE pointers हैं जो आपके program को startup पर automatically मिलते हैं, यही वजह है कि printf() (जो stdout को लिखता है) जैसे functions को पहले कुछ खोलने की ज़रूरत नहीं होती।
उदाहरण: Standard Streams vs Files
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("This goes to stdout automatically");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Basic File Operations
close step skip करना buffered data खोने का risk रखता है जो अभी physically disk पर नहीं लिखा गया, क्योंकि कई I/O operations performance के लिए memory में buffered होते हैं और सिर्फ certain points पर disk पर flush होते हैं, file बंद होने समेत।
उदाहरण: Basic File operations
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("log.txt", "w");
fprintf(fp, "entry");
fclose(fp);
// Print formatted text to the screen
printf("Closed the file");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
File Existence Check करना
यह existence checks के लिए एक common lightweight pattern है, हालांकि इसका एक side effect है: अगर file actually exist करती है, तो आपने इसे अब खोल दिया है और इसे दोबारा बंद करना याद रखना होगा, वरना आप एक file handle leak कर देंगे।
उदाहरण: Checking file existence
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("maybe.txt", "r");
// Check whether fp != NULL
if (fp != NULL) {
// Print formatted text to the screen
printf("File exists");
fclose(fp);
} else {
// Print formatted text to the screen
printf("File does not exist");
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह check किए बिना एक file इस्तेमाल करना कि
fopenनेNULLreturn किया या नहीं। - file को
fcloseसे बंद करना भूल जाना, ताकि data शायद न लिखा जाए। - एक गलत relative path इस्तेमाल करना, ताकि file current directory में न मिले।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: