c++ - fstream and ofstream

后端 未结 2 601
耶瑟儿~
耶瑟儿~ 2021-01-11 16:30

What is the difference between:

fstream texfile;
textfile.open(\"Test.txt\");

and

ofstream textfile;
textfile.open(\"Test.t         


        
2条回答
  •  离开以前
    2021-01-11 17:27

    ofstream only has methods for outputting, so for instance if you tried textfile >> whatever it would not compile. fstream can be used for input and output, although what will work depends on the flags you pass to the constructor / open.

    std::string s;
    std::ofstream ostream("file");
    std::fstream stream("file", stream.out);
    
    ostream >> s; // compiler error
    stream >> s; // no compiler error, but operation will fail.
    

    The comments have some more great points.

提交回复
热议问题