यह C++ में टेक्स्ट फ़ाइल में टेक्स्ट जोड़ने का प्रोग्राम है।
एल्गोरिदम
शुरुआत
उस फ़ाइल a1.txt को आउटपुट फ़ाइल स्ट्रीम क्लास ऑब्जेक्ट के रूप में खोलें ताकि fout फाइल रेफरेंस का उपयोग करके अपेंड मोड में आउटपुट ऑपरेशन किया जा सके
यदि फ़ाइल मौजूद है तो
उस फ़ाइल में टेक्स्ट जोड़ें
fout बंद करें
फ़ाइल "a1.txt" को फ़ाइल की सामग्री पढ़ने के लिए खोलें
फ़ाइल से टेक्स्ट निकालें और टेक्स्ट प्रिंट करें
समाप्ति
उदाहरण कोड
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
fstream f;
ofstream fout;
ifstream fin;
fin.open("a1.txt");
fout.open("a1.txt", ios::app);
if (fin.is_open())
fout << " tutorials point";
cout << "\n डेटा फ़ाइल में जोड़ा गया" << endl;
fin.close();
fout.close();
string word;
f.open("a1.txt");
while (f >> word) {
cout << word << " ";
}
return 0;
}
आउटपुट
डेटा फ़ाइल में जोड़ा गया
मुख्य बिंदु
ios::appमोड फ़ाइल को अपेंड मोड में खोलता है (अंत में जोड़ता है)ofstreamकेवल लिखने के लिए,ifstreamकेवल पढ़ने के लिएfstreamदोनों के लिए उपयोग किया जा सकता है
बेहतर संस्करण
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// अपेंड मोड में खोलें
ofstream fout("a1.txt", ios::app);
if (fout.is_open()) {
fout << " tutorials point";
cout << "डेटा फ़ाइल में जोड़ा गया" << endl;
fout.close();
} else {
cout << "फ़ाइल नहीं खोल सके" << endl;
}
// पढ़कर दिखाएं
ifstream fin("a1.txt");
string word;
while (fin >> word) {
cout << word << " ";
}
fin.close();
return 0;
}