Skip to main content
added 22 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

prasing Parsing text file c++in C++

I have a text file that contains namename,age age,salary salary,hoursWorked hoursWorked,randomText randomText and wereare filled with different delimiters.

textfileText file:

susan:25-2600,28[asd]
mary:21-2200,38[asd]
john:23-3400,46[asd]

insteadInstead 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();

whatWhat are some better strategies other than coding it this way?

sideNoteSide note

I decleareddeclared all the variables to strings because of getlinethe getline() method.

prasing text file c++

I have a text file that contains name,age,salary,hoursWorked,randomText and were filled with different delimiters

textfile

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?

sideNote

I decleared all the variables to strings because of getline method.

Parsing text file in C++

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.

Source Link
dannychua
  • 131
  • 1
  • 1
  • 3

prasing text file c++

I have a text file that contains name,age,salary,hoursWorked,randomText and were filled with different delimiters

textfile

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?

sideNote

I decleared all the variables to strings because of getline method.