0

I apologize if this has been asked before but I am trying to parse an array of characters in away I can obtain filenames.

Here is an example of the character array I end up copying in.

picture1.bmp   file2.txt   random.wtf   dance.png

Notice that there are 3 spaces between each filename.

I want to do something along the sorts of:

  1. Assuming we start at the first character, advance until we reach a space.
  2. Copy everything up to that space into an array at index 0.
  3. Skip 2 spaces (to traverse the 3 space gap).
  4. Go until we hit a space and then copy that into array index 1.

I could do a hack job of this, just curious as to how some of the more advanced programmers would do this as I am here to learn.

1

1 Answer 1

3
std::istringstream iss(the_array);
std::string f1, f2, f3, f4;
iss >> f1 >> f2 >> f3 >> f4;
6
  • Can you explain more perhaps? How does it know to allow for three spaces between the filenames? Dec 22, 2011 at 4:45
  • @PladniusBrooks it skips all spaces (all whitespace actually, including tabs, newlines, etc), be it 1 or 1000 Dec 22, 2011 at 4:48
  • @Pladinus: The default behavior of istream::operator>> on strings is to read whitespace delimited data. When it comes to any whitespace, it stops reading. The next call causes it to skip to the next non-whitespace character. You could have a million spaces, tabs and newlines in there. They would all be skipped over. Dec 22, 2011 at 4:48
  • @BenjaminLindley Thanks to you and Seth Carnegie. One last question. I am getting an "incomplete type is not allowed" as the_array is a pointer to character array. How would I make this code work with that? Dec 22, 2011 at 4:52
  • @PladniusBrooks: Have you included both <string> and <sstream>? Dec 22, 2011 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.