I am new to programming and I have a data.txt
file with the following content:
bumbumbow
1 0 3 9 8
bumbumbum
1 0 3 9 :0
I want to read the line 2: 1 0 3 9 8
and to turn it into integer numbers but I have an error.
What is the problem? Here is my code:
#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;
}
thanks in advance guys