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

C++ का fstream

fstream एक ही file को पढ़ और लिख दोनों सकता है, एक notebook की तरह जिसमें आप लिख भी सकते हैं और पढ़ भी सकते हैं। आप इसमें अलग-अलग जगहों पर jump भी कर सकते हैं।
Syntax
cpp
#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?

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

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

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

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

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

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)

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

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)

cpp
// 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;
}
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. ios::in | ios::out जैसे mode flags भूल जाना।
  2. reads और writes के बीच seek करना या flags clear करना भूल जाना।
  3. यह check न करना कि file successfully open हुई।
🔒

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.