How to make my code stop writing over a current text file

Ok so i have some code that outputs stuff to the screen but everytime i open the program it overwrites the current text file erasing everything that was saved in it. How do i make it so that it just checks to see if there is anything in the file and write the new stuff to a new line?

I dont have any code atm sorry.

Oh and how would i make the code let the user save the file anywhere on there computer? like when your in notepad or something and you click save as, it lets you choose the directory to save it, thats what i want to do. and is there code to create folders too?
Last edited on
closed account (zb0S216C)
Append the data, which basically means add to the end of the file. You need to set the std::ios::app flag when creating a file stream, like so:

 
std::ofstream OutStream("Somefile.txt", std::ios::app);


Wazzak
To append use Framework's solution.

To let a user determine where to save the file, you'll have to get them to enter the path of the desired location in the console, save that as a string and then replace "Somefile.txt" with that string in the example above. If you want the "Save as" dialogue in windows, you'll need to write a windowed program in Win32 and consult the Win32 reference pages.
No i remember saving to directory being easier than that. I had the code at one point bu i deleted the cpp file that had it. I got it off of some coding site. i think it was Dani Web or something. I'll check there but im not sure.
Ok i found it, maybe it was a little different than i thought but here it is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <direct.h>
#include <iostream>
#include <fstream>

int main ()
{

  ofstream write ("c:/myfolder/myfile.txt");

  mkdir("c:/myfolder");

  write << "Sup mo fo";

  write.close();

  return 0;
}
Topic archived. No new replies allowed.