I have a text file that contains name
, age
, salary
, hoursWorked
, randomText
and are filled with different delimiters.
Text file:
susan:25-2600,28[asd] mary:21-2200,38[asd] john:23-3400,46[asd]
Instead of breaking them into individual strings using the code shown below:
string name,age,salary,hoursWorked,randomText; ifstream readFile("textfile.txt"); while(getline(readFile,line)) { stringstream iss(line); getline(iss, name, ':'); getline(iss, age, '-'); getline(iss, salary, ','); getline(iss, hoursWorked, '['); getline(iss, randomText, ']'); } readFile.close();
What are some better strategies other than coding it this way?
Side note
I declared all the variables to strings because of the getline()
method.