Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.67 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to instantiate an fstream if you declare it as a member of a class?
  2. #include <fstream>
  3. class Foo {
  4. Foo();
  5. // not allowed
  6. std::fstream myFile("\temp\foo.txt", fstream::in | fstream::out | fstream::trunc);
  7.  
  8. // allowed
  9. std::fstream myFile;
  10. }
  11.        
  12. // constructor
  13. Foo::Foo() {
  14. // what form of myFile("\temp\foo.txt", fstream::in | fstream::out | fstream::trunc)  can I use here?
  15.  
  16.  
  17. myFile = ???
  18. }
  19.        
  20. Foo::Foo() : myFile("file-name", otherArguments) {
  21.     // other initialization
  22. }
  23.        
  24. Foo::Foo () {
  25.     myFile.open("\temp\foo.txt", fstream::in | fstream::out | fstream::trunc);
  26.  
  27.     if(!myFile.is_open()) {
  28.         printf("myFile failed to open!");
  29.     }
  30.  
  31.     //other initialization
  32. }