1

I can read from a file 1 character at a time, but how do i make it go just one word at a time? So, read until there is a space and take that as a string.

This gets me the characters:

while (!fin.eof()){
  while (fin>> f ){
   F.push_back ( f );
  }
FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
nalbina
  • 11
  • 1
  • 2
  • 1
    Check out this SO post regarding string tokenization: http://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c – gooch May 04 '10 at 18:55
  • `fin.eof()` indicates past failure; it does not tell you whether no more input remains. – Potatoswatter May 04 '10 at 21:40

2 Answers2

3

If your f variable is of type std::string and F is std::vector<std::string>, then your code should do exactly what you want, leaving you with a list of "words" in the F vector. I put words in quotes because punctuation at the end of a word will be included in the input.

In other words, the >> operator automatically stops at whitespace (or eof) when the target variable type is a string.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
2

Try this:

std::string word;
while (fin >> word)
{
    F.push_back(word);
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154