【发布时间】:2014-10-07 05:23:00
【问题描述】:
从单独的文本文件中读取数据时,它不会保留空格,而是看起来像:
Todayyouareyouerthanyou,thatistruerthantrue
当它应该有空格并说:
Today you are youer than you, that is truer than true
这是我到目前为止的代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
std::ifstream inFile;
inFile.open("Rhymes.txt", std::ios::in);
if (inFile.is_open())
{
string word;
unsigned long wordCount = 0;
while (!inFile.eo())
{
cout << word;
inFile >> word;
if (word.length() > 0)
{
wordCount++;
}
}
cout << "The file had " << wordCount << " word(s) in it." << endl;
}
system("PAUSE");
return 0;
}
“Rhymes.txt”有很多短语,例如上面的一个,我只添加 2 个,所以这里就不多说了。他们在这里:
Today you are You, that is truer than true. There is no one alive who is Youer than You.
The more that you read, the more things you will know. The more that you learn, the more places you'll go.
How did it get so late so soon? Its night before its afternoon.
任何帮助或建议将不胜感激!另外我是初学者,所以如果这真的很明显,对不起!
【问题讨论】: