reading a file word by word
I can read from a开发者_Go百科 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 );
}
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.
Try this:
std::string word;
while (fin >> word)
{
F.push_back(word);
}
精彩评论