How to read a file and get words in C++

前端 未结 4 1009
温柔的废话
温柔的废话 2021-01-16 02:06

I am curious as to how I would go about reading the input from a text file with no set structure (Such as notes or a small report) word by word. The text for example might b

4条回答
  •  既然无缘
    2021-01-16 02:47

    Yes. You're looking for std::istream::operator>> :) Note that it will remove consecutive whitespace but I doubt that's a problem here.

    i.e.

    std::ifstream file("filename");
    std::vector words;
    std::string currentWord;
    while(file >> currentWord)
        words.push_back(currentWord);
    

提交回复
热议问题