【发布时间】:2016-04-15 10:42:14
【问题描述】:
我是编程新手,我有一个 data.txt 文件,内容如下:
bumbumbow
1 0 3 9 8
bumbumbum
1 0 3 9 :0
我想读取2: 1 0 3 9 8 行并将其转换为整数,但出现错误。
有什么问题?这是我的代码:
#include <iostream>
#include <fstream>
#include <limits>
#include<string>
#include<vector>
std::fstream& GotoLine(std::fstream& file, unsigned int num) {
file.seekg(std::ios::beg);
for (int i = 0; i < num - 1; ++i) {
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
return file;
}
int main() {
int result, i = 0;
std::vector<int> Array;
using namespace std;
std::fstream file("data.txt");
GotoLine(file, 2);
std::string line2;
std::getline(file, line2);
for (int result; std::getline(file, line2); result = std::stoi(line2))
{
Array.push_back(result);
std::cout << "Read in the number: " << result << "\n\n";
}
return 0;
}
先谢谢各位了
【问题讨论】: