Functions and ofstream

I have one function(s) that's purpose is to write back some information onto a file, and also in my main function, I also have to write back some information to the same file. However, when I run the program my function(s) overwrites what is written in the file from my main function. How would I go about solving this problem?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
include <iostream>
using namespace std;
void function1();
int main()
{
 ofstream out(file);
 out << "hello" << endl;
 function1();
}

function1()
{
 ofstream out(file)
 out << "hi" << endl;
}

I know that since I put "outstream out(file)" it will automatically replace the information already on the file from the main function, but if i just simply put out << "hi"<< endl; in function1, it doesnt work. Thank you.
Instead of creating a new ofstream in function1(), it needs to use the same out that was defined in main(). Pass it by reference as a parameter,
1
2
3
4
function1(ofstream & out)
{
    out << "hi" << endl;
}
Topic archived. No new replies allowed.