开发者

reading a file word by word

开发者 https://www.devze.com 2022-12-28 22:35 出处:网络
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.

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消