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

C File Handling परिचय

File handling यह है कि आपका program files कैसे पढ़ता और लिखता है, पढ़ने या लिखने के लिए एक notebook खोलने जैसा। आप file खोलते हैं, अपना काम करते हैं, और इसे बंद कर देते हैं।
Syntax
c
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

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

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

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

Standard Streams बनाम Files

stdin, stdout, और stderr असल में पहले से open FILE pointers हैं जो आपके program को startup पर automatically मिलते हैं, यही वजह है कि printf() (जो stdout को लिखता है) जैसे functions को पहले कुछ खोलने की ज़रूरत नहीं होती।

उदाहरण: Standard Streams vs Files

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

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

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

File Existence Check करना

यह existence checks के लिए एक common lightweight pattern है, हालांकि इसका एक side effect है: अगर file actually exist करती है, तो आपने इसे अब खोल दिया है और इसे दोबारा बंद करना याद रखना होगा, वरना आप एक file handle leak कर देंगे।

उदाहरण: Checking file existence

c
// 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;
}
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. यह check किए बिना एक file इस्तेमाल करना कि fopen ने NULL return किया या नहीं।
  2. file को fclose से बंद करना भूल जाना, ताकि data शायद न लिखा जाए।
  3. एक गलत relative path इस्तेमाल करना, ताकि file current directory में न मिले।
🔒

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.