How to print your output at end of a file in new line

I want to add a new line in file so i can print output results. Basically i am using same file for input and output. Input already exist in file but now i want to print my new output in new line.


I tried using output.eof or input.eof and vice versa but it just overwrites input that was already present in file.

```
while (!fin.eof()){
fin.get(value);
if (fin.eof()){
fout << 1;
}
}
```
you have to open the file for output and write the value at the end. you can open it in append mode (ios::ate I think, look up the code you want) and just write to it...
ofstream fout(filename, ios::ate);
fout << endl;
fout.close();
tbf i don't understand statement like ios::ate i didn't learn c++ like that.
i did use that but it overwrite existed value in my file.
ios::app seems to work. buti dont understand ios though. thanks man
Use either std::ofstream::ate or std::ofstream::app when opening a file to add content to the end.

Preferable to use std::ofstream::app if you want to write multiple times to your file, it seeks to the end of the stream with each write. std::ofstream::ate only seeks to the end of the stream after the open.

https://en.cppreference.com/w/cpp/io/basic_ofstream/open
i did use that but it overwrite existed value in my file.


Could you post the code that you tried? Saying you used "that" is ambiguous, but posting your code clears up the ambiguity.

Also, please post your code inside code tags:
[code]your code here[/code]
Thanks but ios:app worked.
ios::app is funky looking, but all it is doing is fetching a constant from the global namespace.

it is just like how you will see

std::cout << "hello";

ios::app is an integer code that tells the open() function what to do. Its not complicated, just different syntax to what you are used to seeing.

you may want to re-read Furryguy's post. I am on a journey to get my code out of the 90s and sometimes use older forms for things like this that I haven't mentally updated yet :) Its doing the same thing, but using an updated form, is all.

ios, by the way, is probably just 'Input/Output/Stream' or something to that effect, an acronym.
Last edited on
Topic archived. No new replies allowed.