
Untitled
By: a guest on
May 5th, 2012 | syntax:
None | size: 0.67 KB | hits: 18 | expires: Never
How to instantiate an fstream if you declare it as a member of a class?
#include <fstream>
class Foo {
Foo();
// not allowed
std::fstream myFile("\temp\foo.txt", fstream::in | fstream::out | fstream::trunc);
// allowed
std::fstream myFile;
}
// constructor
Foo::Foo() {
// what form of myFile("\temp\foo.txt", fstream::in | fstream::out | fstream::trunc) can I use here?
myFile = ???
}
Foo::Foo() : myFile("file-name", otherArguments) {
// other initialization
}
Foo::Foo () {
myFile.open("\temp\foo.txt", fstream::in | fstream::out | fstream::trunc);
if(!myFile.is_open()) {
printf("myFile failed to open!");
}
//other initialization
}