tags:

views:

86

answers:

3

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 );
  }
+3  A: 

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
+1, beat me to it.
rcollyer
+2  A: 

Try this:

std::string word;
while (fin >> word)
{
    F.push_back(word);
}
Thomas Matthews
A: 

Thanks everyone I thought it was right, but it turns out I just needed to close my compiler and open it again, something wierd was going on with my current workspace : / thank you for your time, sorry for the silly question :(

nalbina
It's better to edit your original post than provide an answer to it.
Bill
Also, click the checkmark next to the best answer so we don't see it flagged as unanswered.
Potatoswatter