【问题标题】:Accessing data in a text file [closed]访问文本文件中的数据 [关闭]
【发布时间】:2015-02-22 22:37:58
【问题描述】:

我如何访问文本文件并逐字逐句阅读。我了解如何打开文件,但不了解如何逐个提取每个单词。我认为这与数组有关?

【问题讨论】:

标签: c++ arrays fstream


【解决方案1】:

简单地说:

#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 变量将包含文本文件中的每个单词(单词应在文件中用空格分隔)。

【讨论】:

  • 哦,谢谢!这真的很有帮助。 :)
猜你喜欢
  • 2022-01-24
  • 2017-12-31
  • 2014-09-19
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多