+ 1

Adding onto an output file?

Is it possible to use a function prototype to add onto your output file for fstream? Say for example: #include <iostream> #include <fstream> using namespace std; void output(); int main() { ofstream output(“test.txt”); output << “Hello”; output(); return 0; } void output() { output << “world”; } How do I allow it edit the file using function prototypes? Should I refer with ofstream variable? My function has to be like this where it outpus from main and a file. Help please

17th Mar 2018, 9:56 AM
Peter Nguyen
Peter Nguyen - avatar
2 odpowiedzi
+ 2
write prototype of the function like this: void output(ofstream &output); and call in main: output(output); so it works as you want.
26th Mar 2018, 8:17 PM
🇮🇷 M N
0
The way that works is if you used ofstream outfile (“filename.txt”, ios::app). This will allow u to open the same file in different functions
4th May 2018, 12:54 AM
Peter Nguyen
Peter Nguyen - avatar