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

C++ File Handling का परिचय

File handling यह है कि आपका program files कैसे read और write करता है, पढ़ने या लिखने के लिए एक notebook खोलने की तरह। आप file open करते हैं, अपना काम करते हैं, और इसे close करते हैं।
Syntax
cpp
#include <fstream>

std::ofstream file_name("filename.txt");  // open for writing
std::ifstream file_name("filename.txt");  // open for reading
file_name.close();

Streams और Files

C++ fstream library के जरिए file I/O handle करता है, जो cin और cout जैसे ही stream abstraction पर build होती है।

Keyboard से पढ़ने या console पर लिखने की बजाय, file streams disk पर एक file से पढ़ते और लिखते हैं, almost identical syntax इस्तेमाल करते हुए जो आप पहले से console I/O से जानते हैं।

उदाहरण: Streams and Files

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	std::ofstream file("data.txt");
	file << "Hello, file!";
	file.close();
	// Print to the console with cout
	std::cout << "Written using the same << stream style as cout" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक File Open करना

आप एक existing stream object पर open() member function call करके, या stream के constructor में सीधे file path pass करके एक file open कर सकते हैं -- constructor form file को तुरंत open करता है, एक separate call बचाते हुए।

उदाहरण: Opening a File

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	std::ofstream file1;
	file1.open("data.txt");
	std::ofstream file2("data.txt");
	file1 << "Line 1";
	file1.close();
	file2.close();
	// Print to the console with cout
	std::cout << "Opened both ways" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Check करना कि File Open Succeed हुआ

एक file कई वजहों से open होने में fail हो सकती है -- path में एक typo, missing permissions, या एक nonexistent directory -- इसलिए पढ़ने या लिखने की कोशिश करने से पहले हमेशा is_open() call करें (या stream को एक boolean context में check करें)।

यह check skip करना silent failures की ओर ले जाता है जहाँ हर बाद का operation बस कुछ नहीं करता।

उदाहरण: Checking if File Open Succeeded

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	std::ofstream file("data.txt");
	// Check whether file.is_open()
	if (file.is_open()) {
		file << "Success";
		file.close();
	}
	// Print to the console with cout
	std::cout << "Checked before writing" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक File Close करना

C++ automatically एक open file stream close कर देता है जब इसका destructor चलता है, typically scope के अंत में, जो ज़्यादातर resource leaks रोकता है भले ही आप भूल जाएँ।

फिर भी, काम खत्म होने पर explicitly close() call करना अच्छी practice है -- यह scope exit का wait करने की बजाय buffered writes तुरंत disk पर flush करता है।

उदाहरण: Closing a File

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	std::ofstream file("data.txt");
	file << "Content";
	file.close();
	// Print to the console with cout
	std::cout << "Closed explicitly" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

File Open Modes

ios::out (write, existing content overwrite करते हुए) और ios::app (existing को erase किए बिना अंत में append करना) जैसे open mode flags control करते हैं कि existing file content का क्या होता है।

गलत mode चुनना एक common bug है -- append करने का मतलब होने पर accidentally ios::out इस्तेमाल करना silently एक file के contents मिटा देगा।

उदाहरण: File Open Modes

cpp
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>

// Program execution starts in main()
int main() {
	std::ofstream file("data.txt", std::ios::app);
	file << "Appended line\n";
	file.close();
	// Print to the console with cout
	std::cout << "Appended using ios::app" << std::endl;
	// 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 successfully open हुई या नहीं।
  2. file close करना भूल जाना, इसलिए buffered data शायद न लिखा जाए।
  3. गलत relative path इस्तेमाल करना ताकि file न मिले।
🔒

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.