I am trying to get this file program to parse my text from my file but im having a little trouble. I tried to create some code that gets the last position of the colon but I cant seem to figure it out. I made a new getline with a | delimiter, ideally I would like to use the colon again as a separator and store each piece of text thats separated by a colon in its own. I almost got it working but not quite.
The text in my file is this:
Population UK: 66400000 | Test 1
Population France: 66990000 | Test 2
Population Japan: 126800000 | Test 3
What example would you like to view
1) Open File example
2) Parsing Text example
3) Parse Multiple Lines Example
3
UK -- 66400000 Test 1
France -- 66990000 Test 2
Japan -- 126800000 Test 3
void ParsMultipleLines()
{
string filename = "stats.txt";
ifstream input;
input.open(filename);
if (!input.is_open())
{
cout << "Error" << endl;
}
while (input)
{
//input >> ws; //C++ 11 feature that reads and discards white space.
string countryName{};
unsignedlonglong population{};
string test{};
string test2{};
getline(input, countryName, ':');
input >> population;
getline(input, test, ':');
getline(input, test2, '\n');
if (!input) //If the stream reaches the end of the file then stop reading. If this wasnt here we
{ //would have a malformed string on the next blank line.
break;
}
cout << "Country: " << countryName << endl;
cout << "Population: " << population << endl;
cout << "First Test:" << test << endl;
cout << "Second Test:" << test2 << endl;
cout << endl;
}
input.close();
}
Country: UK
Population: 66400000
First Test: Test 1
Second Test: test1
Country: France
Population: 66990000
First Test: Test 2
Second Test: test2
Country: Japan
Population: 126800000
First Test: Test 3
Second Test: test3
I modified the code, I still cant get it to work correctly but its much closer i think. Hopefully. Working with files isnt something i've really delved into, but I want to learn to do it and be able to parse and extract text from it. I'm not really sure whats wrong.
EDIT: I got it mostly working with a few issues. Code below
Ok so I got it almost working, It does what I want BUT, I cannot have spaces inbetween the characters which is an issue, I need to be able to get text with spaces:
@Ch1156,
It is difficult to answer your questions if you keep changing the problem.
Please FIX your input file and DO NOT FURTHER CHANGE IT. The format has changed several times in this thread.
If you want text with spaces between words then you will:
(a) have to agree on some delimiter (:, |, comma whatever) to identify separate fields, since spaces would no longer be available to fulfil that function. If you are going to rely on the population being the only field starting with a digit then say so; we have no idea what you are putting in your "test" fields.
(b) use getline() rather than >> for input, since the latter would react to any space as a delimiter to end a field.
There is nothing wrong with reading an entire field into a std::string first and then separating it subsequently with, e.g, stringstreams or stoi() etc. if you want some fields to be numeric.