std::vector<int> loadNumbersFromFile(std::string name)
{
std::vector<int> numbers;
std::ifstream file;
file.open(name.c_str());
if(!file) {
exit(EXIT_FAILURE);
}
int current;
while(file >> current) {
numbers.push_back(current);
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return numbers;
}
问题是它在 VS 2012 中运行良好,但在 Dev C++ 中它只是读取文件中的第一个数字 - while 循环只执行一次。怎么了?
它应该适用于 .txt 文件。数字输入应该是这样的:
1 3 2 4 5