I'm having trouble filtering the 'commented' lines in a text file. I want to filter the lines beginning with # or // in the text file.
Thanks!
string code, name, year, semester, value, data;
char delimiter = '|';
ifstream ifsUnits;
ifsUnits.open("./data/units.txt");
if (ifsUnits.fail())
cout << "\nError reading from file <units.txt>.";
else
{
while (!ifsUnits.eof())
{
getline(ifsUnits, data);
stringstream ssData(data);
while (ssData.good())
{
getline(ssData, code, delimiter);
getline(ssData, name, delimiter);
getline(ssData, year, delimiter);
getline(ssData, semester, delimiter);
getline(ssData, value, delimiter);
lUnits.push_back(Unit(stoi(code), name, stoi(year), stoi(semester), stoi(value)));
}
}
}
ifsUnits.close();
Text file contents:
//idNumber|name <-- i want to bypass all the lines starting with // 1001|Mary Doe 1002|John Down 1003|John Doe 1004|Marilyn Hendrix
You're already pulling one line into a string at a time. Just check if that string starts with your comment characters and if so, continue
:
while (getline(ifsUnits, data)) // more robust than eof check
{
if ((data.size() > 1 && data[0] == '/' && data[1] == '/')
|| (data.size() > 0 && data[0] == '#')) { continue; }
stringstream ssData(data);
// ...