I have a vector of strings that contain numbers, words, spaces, and even weird characters like quotation marks. I am trying to read it all in using a stringstream so I can separate the doubles from strings.
When I do this:
stringstream contains
4165 This package contains 12 1/2" Screws
Code:
stringstream >> mydouble
stringstream >> mystring
mydouble contains
4165
Then the only thing that gets read into mystring is
This package contains 12 1/2
How can I let it keep the entire line?
Sometimes my file contains this:
4165 This package contains 2 1/2" Screws
65 This package contains 12> 1/2" Screws
This package contains 1 screw
Regrettably, the stream extraction operator
>>is not good at the kind of thing you wish it to do. The right way to do what you want is to fetch the whole line bythen to write code to parse the line.
How to parse the line? Five options come to mind:
<regex.h>.<regex>or TR1<tr1/regex>.None of these however is entirely trivial. If you want to know, for your application, I’d probably go with choice 1, choice 3 or choice 5. Whatever you choose to do, good luck to you.
If you don’t know where to start, why don’t you look up
std::getline(), presented in<string>, andstd::atoi(), presented in<cstdlib>? These should afford you some ideas as to where to go next.