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

C के fopen() और fclose()

fopen एक file खोलता है और fclose इसे बंद करता है, एक notebook खोलने और बंद करने जैसा। आप जो mode चुनते हैं वह बताता है कि आप पढ़ेंगे, लिखेंगे, या जोड़ेंगे।
Syntax
c
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()

c
// 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;
}

fclose() से एक File बंद करना

सिर्फ buffered writes flush करने से आगे, files बंद करना भूल जाना आखिरकार operating system की limit खत्म कर सकता है कि एक single process कितनी files एक साथ open रख सकता है, बाद की fopen() calls को fail होना शुरू करवाते हुए।

उदाहरण: Closing a file with fclose()

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);
	// Print formatted text to the screen
	printf("Closed properly");
	// Return 0 to signal that the program finished successfully
	return 0;
}

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")

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);
	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;
}

Writing Modes ("w", "wb", "a")

w mode आपके इसे खोलते ही चुपचाप किसी भी existing file content को destroy कर देता है, कुछ भी लिखने से पहले भी -- अगर आपको इसमें add करते हुए existing data preserve करना है, a mode safer choice है।

उदाहरण: Writing modes ("w", "wb", "a")

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, "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;
}

कई Open Files Manage करना

हर FILE pointer अपनी file के अंदर अपनी read/write position independently track करता है, इसलिए एक open file पर operations दूसरे पर कोई असर नहीं डालते -- बस पक्का करें कि आपकी हर fopen() call का आखिरकार एक matching fclose() हो।

उदाहरण: Managing multiple open files

c
// 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;
}
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. file pointer इस्तेमाल करने से पहले यह check न करना कि fopen ने NULL return किया।
  2. mode "w" से एक existing file खोलना, जो इसका पिछला content मिटा देता है; append करने के लिए "a" इस्तेमाल करें।
  3. fclose भूल जाना, ताकि buffered output कभी disk तक न पहुंचे।
🔒

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.