A file has some fields with white spaces - problem reading some fields

I am reading a file that contain string and int data.

However, some strings have white spaces in between them yet are of one field. So when i read using getline and space delimiter, only a part of the field is read, this makes the next field to take incorrect data e.g taking the remaining half of the previous field, thus am not able to perform some manipulations. What do i do ? - I have tried soo many references but have found none addressing the problem.

File contain data like:

1
2
3
4
5
6
 28th Nov 2012 United States  1:2 Colombia

29th Oct 2012 Japan 4:2 Saudi Arabia 

14th Sept 2012 Yemen 3:1  Paraguay 


So some string fields like United States yet the two should be read into a single variable. If i use getline without delimiter, it reads even into 1: thus cant use the integer 1 for comparison and sorting. If i use a space delimiter with getline, only United goes into the right field, the string States goes into the int variable when read.

I have even tried to read the U.S string into a char array but the problem remains.



Last edited on
Sounds like a job for regular expressions.
You could try to read each line into a vector of strings using the space as separator - ignoring the leading space at the beginning. You Should end up with 6-8 tokens if the line contains all the informations needed. The first 4 are always what you want. If the 5th one is not the result then it belongs to the first name. The one or two tokens after the result are the second name. I know it's not easy, but this is really a complicated format. Who save it like this?
take a look at string erase
Topic archived. No new replies allowed.