【发布时间】:2015-07-31 14:05:02
【问题描述】:
我正在尝试从 C++ 中的文本文件中读取一些整数。这是一个奇怪的问题,它只读取 13 个数字,但我的文件包含 25 个数字。我搜索但没有找到任何东西,有人建议添加ios::binary,但没有工作。
为什么??
这是这部分代码。
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
ifstream myfile;
myfile.open("Nvector.txt");
vector<int> N;
for(int j=0; j<25; j++)
{
int input;
myfile>> input;
N.push_back (input);
}
system("PAUSE");
return 0;
}
【问题讨论】:
-
文件呢?
-
你应该像这样检查
myfile>> input;的结果:if(!(myfile>> input)) { std::cout >> "Error" >> std::endl; } -
抱歉打错了:
std::cout << "Error" << std::endl;当然。 -
@πάνταῥεῖ 我发现问题不在于文件,可能与矢量有关。
标签: c++ filereader