本文介绍了C ++-fstream和ofstream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
两者之间有什么区别
fstream texfile;
textfile.open("Test.txt");
和
ofstream textfile;
textfile.open("Test.txt");
它们的功能是否相同?
推荐答案
ofstream
仅具有用于输出的方法,因此例如,如果您尝试textfile >> whatever
,它将无法编译. fstream
可以用于输入和输出,尽管有效的方法取决于传递给构造函数/open
的标志.
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.
这篇关于C ++-fstream和ofstream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!