C++ का fstream
In this page:
#include <fstream>
std::fstream file_name("filename.txt", std::ios::in | std::ios::out);
file_name << value;
file_name >> variable;
file_name.close();
fstream क्या है?
std::fstream ifstream और ofstream की capabilities को एक single bidirectional stream object में combine करता है, आपको एक file के लिए दो separate stream objects की ज़रूरत के बिना same handle से पढ़ने और लिखने दोनों देते हुए।
उदाहरण: What is fstream?
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::fstream file("data.txt", std::ios::out);
file << "Hello";
file.close();
// Print to the console with cout
std::cout << "Written with a single bidirectional fstream" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Sequentially Read और Write करना
एक stream internally अपनी read/write position track करता है, इसलिए एक file में लिखने के बाद आप cursor पहले move किए बिना तुरंत वो वापस नहीं पढ़ सकते जो आपने लिखा -- पढ़ने से पहले read pointer reposition करने के लिए seekg() call करें (commonly seekg(0) से start पर वापस)।
उदाहरण: Reading and Writing Sequentially
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
file << "42";
file.seekg(0);
int value;
file >> 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
fstream से Append करना
ios::app flag के साथ एक fstream open करना नए writes को file के अंत में append करता है जबकि same open stream से file में कहीं और भी reads allow करता है -- उन programs के लिए useful जिन्हें फिर से file open किए बिना नई entries log और पुरानी inspect दोनों करनी हैं।
उदाहरण: Appending with fstream
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::fstream file("data.txt", std::ios::app | std::ios::in);
file << "Appended";
// Print to the console with cout
std::cout << "Appended while still allowing reads" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pointer Manipulation (seekg और seekp)
fstream actually दो separate position indicators maintain करता है: seekg() "get" (read) cursor move करता है, और seekp() "put" (write) cursor move करता है।
आप दोनों में से किसी को एक absolute byte offset या file की शुरुआत, current position, या अंत से relative offset pass कर सकते हैं।
उदाहरण: Pointer Manipulation (seekg & seekp)
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
file << "0123456789";
file.seekg(3);
char c;
file.get(c);
// Print to the console with cout
std::cout << c << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Positions बताना (tellg और tellp)
tellg() और tellp() respectively read और write cursors का current byte offset report करते हैं, उन्हें move किए बिना। एक file का total size measure करने का common trick है अंत तक seekg करना और फिर वह final position पढ़ने के लिए tellg() call करना।
उदाहरण: Telling Positions (tellg & tellp)
// Include std::cout and std::cin
#include <iostream>
// Include file streams
#include <fstream>
// Program execution starts in main()
int main() {
std::fstream file("data.txt", std::ios::in | std::ios::out | std::ios::trunc);
file << "Hello";
file.seekg(0, std::ios::end);
// Print to the console with cout
std::cout << "Size: " << file.tellg() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
ios::in | ios::outजैसे mode flags भूल जाना।- reads और writes के बीच seek करना या flags clear करना भूल जाना।
- यह check न करना कि file successfully open हुई।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: