Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我尝试使用 fstream 从文本文件中读取数据,但得到了错误的数据。
ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in"); int number; fin >> number; cout << number;
test.in简直就是12。 cout读取4273190。 有人可以解释为什么会这样以及如何解决吗?
test.in
12
cout
4273190
最可能的原因是文件打开失败。打开后检查状态,阅读后也可以;对于一个简单的测试,做这样的事情:
ifstream fin ("C:\\Users\\rEgonicS\\Documents\\test.in"); if (!fin) cout << "File open failed\n"; int number; fin >> number; if (!fin) cout << "File read failed\n"; cout << number;
这可能会为正在发生的事情提供进一步的线索。