C 仅在有更多内容时从文件中读取

C++ read from file only when there is more content

本文关键字:文件 读取      更新时间:2023-10-16

如何检查C 中文本文件中是否有更多内容以及是否继续读取它?我试图从文本文件中读取一些单词,但未指定单词的数量。

在此处查看此链接。

另外,STD ::是对您正在调用的代码名称空间的引用。当您在标准库中包含一个文件时,例如字符串,向量,fstream,iostream,您需要声明您的文件将使用使用命名空间std的命名空间std;或者,您将std ::附加到方法或变量。

使用std::vectorstd::string。使用正确的读取文件的形式:

std::string word;
std::vector<std::string> word_database;
while (text_file >> word)
{
  word_database.push_back(word);
}
std::cout << "Words read: " << word_database.size() << "n";