উত্তর:
আপনাকে অ্যাপেন্ড ওপেন মোডের মতো নির্দিষ্ট করতে হবে
#include <fstream>
int main() {
std::ofstream outfile;
outfile.open("test.txt", std::ios_base::app); // append instead of overwrite
outfile << "Data";
return 0;
}
std::ofstream::out | std::ofstream::appপরিবর্তে ব্যবহার করতে পারেন std::ios_base::app? cplusplus.com/references/fstream/ofstream/open
outব্যবহার করার সময় আপনার পতাকাটি স্পষ্টভাবে উল্লেখ করার দরকার নেই std::ofstream, এটি সর্বদা পতাকা আপনার জন্য স্পষ্টভাবে ব্যবহার করে out। inজন্য পতাকা একই std::ifstream। আপনি যদি পরিবর্তে ব্যবহার করে থাকেন তবে আপনাকে ফ্ল্যাগ inএবং outফ্ল্যাগগুলি স্পষ্টভাবে উল্লেখ করতে std::fstreamহবে।
আমি এই কোডটি ব্যবহার করি। এটি নিশ্চিত করে যে এটি উপস্থিত না থাকলে ফাইলটি তৈরি হয়ে যায় এবং ত্রুটি পরীক্ষার বিট যোগ করে।
static void appendLineToFile(string filepath, string line)
{
std::ofstream file;
//can't enable exception now because of gcc bug that raises ios_base::failure with useless message
//file.exceptions(file.exceptions() | std::ios::failbit);
file.open(filepath, std::ios::out | std::ios::app);
if (file.fail())
throw std::ios_base::failure(std::strerror(errno));
//make sure write fails with exception if something is wrong
file.exceptions(file.exceptions() | std::ios::failbit | std::ifstream::badbit);
file << line << std::endl;
}
#include <fstream>
#include <iostream>
FILE * pFileTXT;
int counter
int main()
{
pFileTXT = fopen ("aTextFile.txt","a");// use "a" for append, "w" to overwrite, previous content will be deleted
for(counter=0;counter<9;counter++)
fprintf (pFileTXT, "%c", characterarray[counter] );// character array to file
fprintf(pFileTXT,"\n");// newline
for(counter=0;counter<9;counter++)
fprintf (pFileTXT, "%d", digitarray[counter] ); // numerical to file
fprintf(pFileTXT,"A Sentence"); // String to file
fprintf (pFileXML,"%.2x",character); // Printing hex value, 0x31 if character= 1
fclose (pFileTXT); // must close after opening
return 0;
}
আপনি এটি এইভাবে করতে পারে
#include <fstream>
int main(){
std::ofstream ost {outputfile, std::ios_base::app};
ost.open(outputfile);
ost << "something you want to add to your outputfile";
ost.close();
return 0;
}
ofstreamকনস্ট্রাক্টরের কাছে ফাইলের নাম পাস করার সাথে সাথে ফাইলটি ওপেন হয় , open()তারপরে কল করা অপ্রয়োজনীয়।
"সি ++ প্রোগ্রামিং ইন ইজি স্টেপস" নামে একটি বই থেকে উত্তরের জন্য আমার কোড পেয়েছি। নীচে কাজ করা উচিত।
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
ofstream writer("filename.file-extension" , ios::app);
if (!writer)
{
cout << "Error Opening File" << endl;
return -1;
}
string info = "insert text here";
writer.append(info);
writer << info << endl;
writer.close;
return 0;
}
আমি আশা করি এটা আপনাকে সাহায্য করবে।
আপনি একটি ব্যবহার করতে পারেন fstreamএবং std::ios::appপতাকা দিয়ে এটি খুলতে পারেন । নীচের কোডটি দেখুন এবং এটি আপনার মাথা পরিষ্কার করা উচিত।
...
fstream f("filename.ext", f.out | f.app);
f << "any";
f << "text";
f << "written";
f << "wll";
f << "be append";
...
আপনি এখানে মুক্ত মোডগুলি সম্পর্কে আরও তথ্য পেতে পারেন এবং প্রায় fstreams এখানে ।