I have found several similar questions to read file line by line 1,2, I tried to follow them but it seems my code does not work correctly.
here is my try:
Input file
0 3 5 7
1 5 6 7 9
2 3 6 7 8
3 0 2 7 9
4 6 8
5 0 1 7
6 1 2 4 9
7 0 1 2 3 5 8
8 2 4 7
9 1 3 6
The code
int main() {
std::vector<std::vector<int>> a;
a.resize(10);
std::string filename = "data/a.adjlist";
std::ifstream file(filename);
if (file.is_open()) {
std::string line;
int counter = 0;
while (getline(file, line)) {
printf("%s \n", line.c_str());
std::istringstream iss(line);
int value;
while(iss >> value)
a[counter].push_back(value);
counter ++;
}
}
file.close();
for (int i=0; i<a.size(); i++) {
for (int j=0; j<a[0].size(); j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}
}
Output
0 3 5 7
1 5 6 7
2 3 6 7
3 0 2 7
4 6 8 7
5 0 1 7
6 1 2 4
7 0 1 2
8 2 4 7
9 1 3 6