【发布时间】:2015-02-22 22:37:58
【问题描述】:
我如何访问文本文件并逐字逐句阅读。我了解如何打开文件,但不了解如何逐个提取每个单词。我认为这与数组有关?
【问题讨论】:
-
Split a string in C++? 的可能重复项
我如何访问文本文件并逐字逐句阅读。我了解如何打开文件,但不了解如何逐个提取每个单词。我认为这与数组有关?
【问题讨论】:
简单地说:
#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 变量将包含文本文件中的每个单词(单词应在文件中用空格分隔)。
【讨论】: