C के fopen() और fclose()
In this page:
FILE *file_pointer = fopen("filename.txt", "mode"); // "r", "w" or "a"
if (file_pointer == NULL) {
// could not open the file
}
fclose(file_pointer);
fopen() से एक File खोलना
Common modes में "r" (read), "w" (write, existing content truncate करते हुए), और "a" (append) शामिल हैं -- इसे इस्तेमाल करने से पहले हमेशा returned pointer को NULL के against check करें, क्योंकि एक missing file या insufficient permissions से fopen() अन्यथा चुपचाप fail हो जाएगा।
उदाहरण: Opening a file with fopen()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("data.txt", "w");
// Check whether fp == NULL
if (fp == NULL) {
// Print formatted text to the screen
printf("Failed to open");
// Send 1 back to the caller
return 1;
}
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
fclose() से एक File बंद करना
सिर्फ buffered writes flush करने से आगे, files बंद करना भूल जाना आखिरकार operating system की limit खत्म कर सकता है कि एक single process कितनी files एक साथ open रख सकता है, बाद की fopen() calls को fail होना शुरू करवाते हुए।
उदाहरण: Closing a file with fclose()
// 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);
// Print formatted text to the screen
printf("Closed properly");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reading Modes ("r", "rb")
Text mode (r) platform-specific newline translation perform कर सकता है (जैसे Windows पर \r\n को \n में बदलना), जो images जैसी binary data corrupt करता है -- binary mode (rb) इस translation को disable करता है और bytes को exactly stored के मुताबिक पढ़ता है।
उदाहरण: Reading modes ("r", "rb")
// 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);
fp = fopen("data.txt", "r");
char buffer[10];
fgets(buffer, 10, fp);
// Print formatted text to the screen
printf("%s", buffer);
fclose(fp);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Writing Modes ("w", "wb", "a")
w mode आपके इसे खोलते ही चुपचाप किसी भी existing file content को destroy कर देता है, कुछ भी लिखने से पहले भी -- अगर आपको इसमें add करते हुए existing data preserve करना है, a mode safer choice है।
उदाहरण: Writing modes ("w", "wb", "a")
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp = fopen("data.txt", "w");
fprintf(fp, "First");
fclose(fp);
fp = fopen("data.txt", "a");
fprintf(fp, "Second");
fclose(fp);
// Print formatted text to the screen
printf("Appended without erasing");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई Open Files Manage करना
हर FILE pointer अपनी file के अंदर अपनी read/write position independently track करता है, इसलिए एक open file पर operations दूसरे पर कोई असर नहीं डालते -- बस पक्का करें कि आपकी हर fopen() call का आखिरकार एक matching fclose() हो।
उदाहरण: Managing multiple open files
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
FILE *fp1 = fopen("a.txt", "w");
FILE *fp2 = fopen("b.txt", "w");
fprintf(fp1, "File A");
fprintf(fp2, "File B");
fclose(fp1);
fclose(fp2);
// Print formatted text to the screen
printf("Both files written independently");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- file pointer इस्तेमाल करने से पहले यह check न करना कि
fopenनेNULLreturn किया। - mode
"w"से एक existing file खोलना, जो इसका पिछला content मिटा देता है; append करने के लिए"a"इस्तेमाल करें। fcloseभूल जाना, ताकि buffered output कभी disk तक न पहुंचे।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: