read word by word from file in C++

后端 未结 5 893
清歌不尽
清歌不尽 2020-12-01 12:53

this function should read a file word by word and it does work till the last word, where the run stops

void readFile(  )
{
    ifstream file;
    file.open          


        
5条回答
  •  孤街浪徒
    2020-12-01 13:17

    First of all, don't loop while (!eof()), it will not work as you expect it to because the eofbit will not be set until after a failed read due to end of file.

    Secondly, the normal input operator >> separates on whitespace and so can be used to read "words":

    std::string word;
    while (file >> word)
    {
        ...
    }
    

提交回复
热议问题