ifstream arrays and functions

Hello who all reads this question. I am curious if the ifstream declare is the same method as ofstream when you declare a function/prototype? IE void print(int a, int b, ofsteam& outfile, ifstream& infile)?
Yes, try it out yourself
Hello CodeNovice01,

In by chance you are not aware of this "ofstream" and "ifstream" are only references to the original streams that were defined in the calling function.

Now "outFile" and "inFile" can use a different name in the function or as you have you can use the same bane because the scope of the original function has lost scope anr the new scope if now the "print" function. Which allows yo to use the same names as in the original function. either these variables are still a reference to the original definition.

In the example you have given you have done this correctly.

Hope that helps,

Andy
Sure, you COULD use that prototype as shown. However, you will massively increase the generality of your code if you treat all streamed io the same way and replace ofstream by ostream, ifstream by istream: for what you plan to do in print I doubt that there is much that necessarily needs to go to or from a file.

Thus:
void print(int a, int b, ostream& out, istream& in)

Provided you declare them outside, you can call it with any ifstream or ofstream, since these are references to derived classes, or with any other istream or ostream (such as cin and cout).
Last edited on
Topic archived. No new replies allowed.