Writing to files. How?

I have an array of objects, whose parameters I can transform into one string, I now want to write those strings to a text file for later use.
Th thing is I have this code: (simplified)

int function(){
while (repeat=true){

if (day=="Monday"){
//requests data then writes it to a file using this code:
string params = parameters of the object;
ofstream myfile;
myfile.open ("hor-" + name + ".txt");
myfile << params << endl;

myfile.close();
}

}


}



The problem is: this is supposed to request the user to enter data every iteration then write all that down into one file, all in different lines, but instead what it does is it writes one line, then in the next iteration it writes the introduced data into the same line, so it replaces the first line all the time.
Is there a way to choose in what line to write to? So that I can set a counter for every iteration to be the number of the line in which to write to
Last edited on
but instead what it does is it writes one line, then in the next iteration it writes the introduced data into the same line, so it replaces the first line all the time.

No, what it does is:

1. Open the file erasing anything in the file.
2. Write one line.
3. Close the file.
4. Repeat from line one.

What you need to do is open the file so that it doesn't erase the file contents, you will need to use the optional second parameter. Something like myfile.open(your_file_name, ios::app);.

I will try it now, thank you in advance. Lets see, it........WORKS!
(I actually found one bug but it may not have anything to do with this anyway, thank you!
Registered users can post here. Sign in or register to post.