need help passing a stream to a function

I'm trying to get a function to use and add to an fstream, but it doesn't seem to work. Here's what I've got so far:

1
2
3
4
5
6
7
void append_log() {
    fstream out(logfile.c_str(), ios::out | ios::app);
    out << "info added to stream";
    add_to_stream(out);
    out << "\n";
    out.close();
}


where the add_to_stream function looks like this:

1
2
3
void add_to_stream(fstream & out) {
    out << "adding some text";
}


yet for some reason this only adds "info added to stream:" but doesn't add the "adding some text". I have no idea where I'm going wrong so it'd be great to get some feedback!
You are creating your fstream object within the append_log() function. How are you passing a reference to that object to add_to_stream()?
@OP, I see nothing wrong with that code. Try compiling
1
2
3
4
5
6
7
8
9
10
#include <fstream>

void print( std::fstream& out ) { out << "world"; }

int main() {
   std::fstream file("hello.txt", std::ios::out | std::ios::app );
   file << "hello ";
   print( file );
   file.close();
}


@kooth, line 4. What are you talking about ?
hamsterman, without ausairman's main routine, I mistakingly thought that he was callin add_to stream() outside of append_log() (which wouldn't work.) My bad!
hamsterman, that snippet ran fine. I did some looking and it turns out I had a booboo elsewhere that was causing my add_to_stream equivalent function not to add to the stream. Thanks for the help :)
Topic archived. No new replies allowed.