0

I have an input file that has multiple lines in the current format:

[message1]/[message2]/[message3]

I have to extract [message#] and put each into a separate container (vector, array, etc). Is there an easy way to do this with a stringstream?

Note: [message#] will NOT contain the character '/'.

I have initially tried to do this:

istringstream iss;
iss >> message1 >> "/" >> message2 >> "/" >> message3;

but it wouldn't work. Any hints appreciated.

  • Probably, but an easier solution may be to use a split function. boost has [one implementation](http://www.boost.org/doc/libs/1_57_0/doc/html/string_algo/reference.html#header.boost.algorithm.string.split_hpp), but it isn't that hard to write yourself and could be a good exercise. – clcto Mar 17 '15 at 23:08
  • Unfortunately, it's for a class and we're not allowed to use boost. I will try to read up on it and see if I can implement it myself. Thanks! – Kelvin Tam Mar 17 '15 at 23:11
  • Are you required to use stringstream? – DigitalNinja Mar 17 '15 at 23:14
  • See the [`std::string` documentation](http://www.cplusplus.com/reference/string/string/). `find_first_of()` will come in handy – clcto Mar 17 '15 at 23:14
  • Is that all of your code? – DigitalNinja Mar 17 '15 at 23:19
  • possible duplicate of [Split a string in C++?](http://stackoverflow.com/questions/236129/split-a-string-in-c) –  Mar 17 '15 at 23:20
  • Also, you have to put each string in a separate container? That seems like a strange requirement. –  Mar 17 '15 at 23:21
  • I asked about possible missing code earlier because I think you should be assigning your file buffer to `iss` like this `istringstream iss(inputFile);`? – DigitalNinja Mar 17 '15 at 23:26
  • possible duplicate of [How can I std::getline() a string?](http://stackoverflow.com/questions/29108902/how-can-i-stdgetline-a-string) – Julian Mar 17 '15 at 23:27

0 Answers0