Passing an open file between functions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    //Writefile
    ofstream* WriteFile(string filename)
    {
        ofstream* f;
        f->open("test",ios::out );
        return f;
    }
    //WriteLine
    void WriteLine(ofstream* file, string stri)
    {
       *file << stri.c_str();
    }

    //CloseFile
    void CloseFile(ofstream* file)
    {
       file->close();
    }


this has kept me crazy for a few hours, i have written these simple functions as a test so i can pass an open file between functions.

i keep getting the message>
std::ios_base::failure'what(): basic_ios::clear

what am i doing wrong? I have tracked it down to the .open in writefile.
Last edited on
You didn't create an object in WriteFile, modify line 4 to ofstream* f = new ofstream;
In the same function you aren't using the filename argument
In WriteLine you don't need to call c_str() String should be string (no capital S)
AHHH new!!!

Thanks.

(i disabled the string to make sure it was not the string class causing the problem. String is actually a typedef of irrlichts' irr::core::stringc. This was also cayusing a problem so i am back to string. :)

I also added delete file to closefile is this right? or does .close do this for me? )I have no errors using delete :/
Last edited on
I think using delete in CloseFile is a good idea as you likely won't use the object pointed by file again
Why not make the program OO, and make the filestream a member variable?
Topic archived. No new replies allowed.