combining file open statement and declaration?


Isn't this how one would combine the declaration and open statement for a file ?

ofstream fout.open("ofile");

???


I get "... syntax error : missing ';' before '.' ... "
No. There are two methods(from what I know, might be wrong):

1
2
ofstream mystream;
mystream.open("file");


And the better:

 
ofstream mystream("file");


The i/o/fstream classes have constructors that automatically open the file which have the same parameters as the open() fucntion so you can do it like the above.

http://www.cplusplus.com/reference/iostream/ofstream/ofstream/
http://www.cplusplus.com/reference/iostream/ifstream/ifstream/
http://www.cplusplus.com/reference/iostream/fstream/fstream/
Last edited on
Topic archived. No new replies allowed.