Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing a filestream as a parameter ?

Status
Not open for further replies.

SteveBrett

Programmer
Apr 24, 2000
107
MT
Can this be done ?

I need to create some files and want functions to create a header and footer and main body.

I was thinking of creating a filestream (ostream) and then some functions that i could include.

however when i pass the address of the filestream it won't build. i've tried passing the address or the stream and also changing the param to an int and it worked. it just won't work with a file.

any ideas ? this is unmanaged c++ code, a win 32 app with ATL support. i'm a bit knew to the c++ stuff (have done loads of c#) so bear with me ...

TIA

Steve
 
You should probably post the code that doesn't work, ideally a small but compilable program that demonstrates the issue, but if that's not possible then just post the function declaration and definition, the creation of the ofstream, the code that calls the function and the error.

A common way to pass an fstream around is:
Code:
void UseStream(std::fstream& strm) {
  // ...
}

...

std::fstream myFile("data.txt");
UseStream(myFile);
 
Thanks - I was missing the std:: in my function definition.

Changed it and it works ...

Many thanks

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top