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

C++ में ifstream और ofstream

ofstream files में लिखता है और ifstream इनसे पढ़ता है, एक notebook के लिए एक pen और reading glasses की तरह। ये cout और cin जैसे ही काम करते हैं।
Syntax
cpp
#include <fstream>

std::ofstream out("filename.txt");
out << value;
out.close();

std::ifstream in("filename.txt");
in >> variable;
in.close();

ofstream से Write करना

std::ofstream files में लिखने के लिए specialized है, और insertion operator (<<) reuse करता है जो आप पहले से std::cout से जानते हैं -- एक बार file open हो जाए, file << "text" exactly उसी तरह इसमें लिखता है जैसे cout << "text" console पर लिखता है।

उदाहरण: Writing with ofstream

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" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

ifstream से Read करना

std::ifstream files से पढ़ने के लिए specialized है, extraction operator (>>) इस्तेमाल करते हुए बिल्कुल std::cin की तरह। यह एक बार में एक whitespace-delimited token पढ़ता है -- numbers, words -- इन्हें automatically target variable के type में convert करते हुए।

उदाहरण: Reading with ifstream

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

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

Line by Line Read करना

क्योंकि >> whitespace पर रुक जाता है, यह एक call में spaces वाली एक line नहीं पढ़ सकता -- "Hello World" पढ़ना "Hello" पर रुक जाएगा। std::getline(stream, str) पूरी line पढ़ता है, internal spaces समेत, अगली newline तक, जो sentence- या record-based file formats के लिए चाहिए होता है।

उदाहरण: Reading Line by Line

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

// Program execution starts in main()
int main() {
	std::ofstream out("data.txt");
	out << "Hello World";
	out.close();
	std::ifstream in("data.txt");
	std::string line;
	std::getline(in, line);
	// Print to the console with cout
	std::cout << line << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

ofstream से Append करना

plain ofstream से एक file open करना इसे truncate कर देता है, लिखना शुरू होने से पहले किसी भी existing content को discard करते हुए।

इसकी बजाय ios::app flag pass करना हर write को existing file के अंत में position करता है, आपको पहले से saved चीज़ को destroy किए बिना नया data (जैसे log entries) add करने देते हुए।

उदाहरण: Appending with ofstream

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 text";
	file.close();
	// Print to the console with cout
	std::cout << "Appended without truncating" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Safe File Writing Practices

सिर्फ is_open() check करने से आगे, हर read या write के बाद stream की state inspect करें -- एक failed write (उदाहरण के लिए, एक full disk से) fail bit set करता है भले ही stream successfully open हुआ हो, और code जो यह check नहीं करता वह silently data खो सकता है।

उदाहरण: Safe File Writing Practices

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 << "Some data";
	// Check whether file.fail()
	if (file.fail()) {
		// Print to the console with cout
		std::cout << "Write failed" << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Write succeeded" << std::endl;
	}
	file.close();
	// 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. ofstream या ifstream इस्तेमाल करने से पहले यह check न करना कि यह successfully open हुआ, इसलिए एक missing file unnoticed रह जाती है।
  2. एक existing file को default mode में ofstream से open करना, जो इसका old content erase कर देता है; append के लिए ios::app इस्तेमाल करें।
  3. while (!file.eof()) से lines पढ़ना, जो अक्सर last line दो बार process करता है; इसकी बजाय getline पर loop करें।
🔒

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.