访问文本文件中的数据

Accessing data in a text file

本文关键字:数据 文件 文本 访问      更新时间:2023-10-16

如何访问文本文件并逐字逐句地查看。我知道如何打开文件,但不知道如何一个字一个字地提取出来。我认为这与数组有关?

简单地说:

#include <fstream>
#include <iostream>
int main()
{
  std::fstream file("table1.txt");
  std::string word;
  while (file >> word)
    {
      // do whatever you want, e.g. print:
      std::cout << word << std::endl;
    }
  file.close();
  return 0;
}

word变量将包含文本文件中的每一个单词(单词应在文件中用空格分隔)。