C++ में ifstream और ofstream
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
ifstream से Read करना
std::ifstream files से पढ़ने के लिए specialized है, extraction operator (>>) इस्तेमाल करते हुए बिल्कुल std::cin की तरह। यह एक बार में एक whitespace-delimited token पढ़ता है -- numbers, words -- इन्हें automatically target variable के type में convert करते हुए।
उदाहरण: Reading with ifstream
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
ofstreamयाifstreamइस्तेमाल करने से पहले यह check न करना कि यह successfully open हुआ, इसलिए एक missing file unnoticed रह जाती है।- एक existing file को default mode में
ofstreamसे open करना, जो इसका old content erase कर देता है; append के लिएios::appइस्तेमाल करें। 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: