'How to read from a file that has numbers in the beginning of each line of the string and extract numbers as an idx then store in a vector. Using C++
Hello I'm working on a school project and I'm trying to read from a file with these contents:
the idx of question, the corresponding concept 1 Arrays Hold Multiple Values 1 Pointer Variables 2 Arrays as Function Arguments 3 Comparing Pointers 4 Pointer Variables 5 Pointer Variables 6 Initializing Pointers 7 Pointers as Function Parameters 8 Arrays as Function Arguments 8 Pointer Variables 9 Arrays Hold Multiple Values 10 Pointer Variables
The goal is to store the numbers on the beginning of the string into a vector container to hold its values along with the rest of the string as the corresponding concepts.
Note: Each number represents a question in a quiz and it may include more than one concept, which is separated in a different line in the file. So mapping the two contents into a string with the same number 1 for example is totally fine.
I tried 2d vector and 1d vector but my problem is I don't know how I can split the string and detect the integers in the beginning also with two or three digits integers the problem will be more complex.
Please any help will be much appreciated!!!!
Here what I tried:
void MapQC::setMapQC()
{
string line;
string idxString;
fstream inFile;
int numOfLines = 0;
int idx;
inFile.open("database/map-question-concept.txt", ios::in);
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile, line);
vector<string> row;
for (int i = 0; i <= numOfLines; i++)
{
if (getline(inFile, line, ' '))
{
// cout << line << endl;
row.push_back(line);
}
}
mapQCVec.push_back(row);
numOfLines ++;
cout <<"row at: " << row[1] << endl;
}
}
inFile.close();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
