我希望将一个段落从单独的文本文件拆分为它们自己的字符串时提供一些建议/帮助。我到目前为止的代码只是计算该段中的单词总数,但我想分割它,因此每行是1个句子,然后计算该句子/行中有多少单词,然后将其放入其自己的数组中,以便我可以用特定的知觉/线条做其他事情。下面是我的代码明智:将单独的文本文件的段落拆分为不同的字符串
#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())
{
inFile >> word;
if (word.length() > 0)
{
wordCount++;
}
}
cout << "The file had " << wordCount << " word(s) in it." << endl;
}
system("PAUSE");
return 0;
}
单独的文本文件被称为“Rhymes.txt”,并且包含:
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.
Today was good. Today was fun. Tomorrow is another one.
And will you succeed? Yes indeed, yes indeed! Ninety-eight and three-quarters percent guaranteed!
Think left and think right and think low and think high. Oh, the things you can think up if only you try!
Unless someone like you cares a whole awful lot, nothing is going to get better. It's not.
I'm sorry to say so but, sadly it's true that bang-ups and hang-ups can happen to you.
所以,第一行是自己的句子,当代码执行它会说:
The line has 19 words in it
我也有点困惑,因为我怎么会这样做。我已经看到了将句子拆分成单词的例子,但我找不到任何我可以真正理解的与我所要求的有关的东西。
这是 “独立的”,而不是 “独立”。 – 2014-10-05 09:20:13