【发布时间】:2015-03-10 19:12:06
【问题描述】:
所以我目前有一个工作程序,可以成功找到并显示文本文件中的所有字符。我现在希望能够读取整个单词而不是字符,然后想将每个单词存储到一个数组中,但我什至不知道如何读取整个单词。
我当前的字符代码
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream infile("input.txt");
if (!infile)
{
cout << "ERROR: ";
cout << "Can't open input file\n";
}
infile >> noskipws;
while (!infile.eof())
{
char ch;
infile >> ch;
// Useful to check that the read isn't the end of file
// - this stops an extra character being output at the end of the loop
if (!infile.eof())
{
cout << ch << endl;
}
}
system("pause");
}
【问题讨论】:
-
可以将
>>改为char,而不是将>>改为std::string -
请阅读此声明的链接:
while (!infile.eof())
标签: c++