I have a program already formed that has a string that I want to stream to the end of an existing text file. All of what little I have is this: (C++)
void main(
Open your stream as append, new text written to it will be written at the end of the file.
Open your file using std::ios::app
#include <fstream>
std::ofstream out;
// std::ios::app is the open mode "append" meaning
// new data will be written to the end of the file.
out.open("myfile.txt", std::ios::app);
std::string str = "I am here.";
out << str;
To append contents to the end of files, simply open a file with ofstream
(which stands for out file stream) in app
mode (which stands for append).
#include <fstream>
using namespace std;
int main() {
ofstream fileOUT("filename.txt", ios::app); // open filename.txt in append mode
fileOUT << "some stuff" << endl; // append "some stuff" to the end of the file
fileOUT.close(); // close the file
return 0;
}
I hope that isn't your whole code because if it is, there's lots of things wrong with it.
The way you would write out to a file looks something like this:
#include <fstream>
#include <string>
// main is never void
int main()
{
std::string message = "Hello world!";
// std::ios::out gives us an output filestream
// and std::ios::app appends to the file.
std::fstream file("myfile.txt", std::ios::out | std::ios::app);
file << message << std::endl;
file.close();
return 0;
}