I need to use C++ to read in text with spaces, followed by a numeric value.
For example, data that looks like:
text1 1.0 text two 2.1 text2 again 3.1
can’t be read in with 2 'infile >>' statements. I’m not having any luck with getline either. I ultimately want to populate a struct with these 2 data elements. Any ideas?
The standard IO library isn’t going to do this for you alone, you need some sort of simple parsing of the data to determine where the text ends and the numeric value begins. If you can make some simplifying assumptions (like saying there is exactly one text/number pair per line, and minimal error recovery) it wouldn’t be too bad to getline() the whole thing into a string and then scan it by hand. Otherwise, you’re probably better off using a regular expression or parsing library to handle this, rather than reinventing the wheel.