【发布时间】:2014-06-30 01:54:15
【问题描述】:
#include <stream>
#include <iostream>
#include <stream>
#include <iosfwd>
#include <string>
using namespace std;
int main(int argc, const char * argv[]) {
string op = "original";
ifstream file;
file.open("sample.txt");
if(!file) {
cout << "Error: unable to open file." << endl;
}
else {
while(!file.eof()) {
file >> op;
cout << op << endl;
}
}
file.close();
return 0;
}
sample.txt 仅包含单词five
我在 Visual Studio 中运行这段代码,输出是
five
我在 Xcode 中运行完全相同的代码(直接复制和粘贴,并且肯定会添加 data.txt),并且在命令行中打印的是
original
我看到的错误是
file >> op
从未真正在 Xcode 中工作过。为什么?
【问题讨论】:
-
如何打印任何东西?这绝对不是有效的 C++。
-
请将真实代码粘贴到您的问题中。这不是你的程序。
-
while(!file.eof()){stackoverflow.com/questions/5605125/… -
@Afonso ASCII 和 UTF-8 对于“五”是一样的。
-
@user3703783,没有人知道包含的内容应该是什么,尤其是编译器。而
<stream>不是标准标题(有iostream、istream、ostream、sstream、strstream、fstream,也许还有其他)。但真诚地,感谢您回来。