我不知道如何阅读几个段落,而不是将每个单词放在结构中,而不是计算有多少独特单词。字符串问题C++
我知道如何读取数据只是没有从一个字符串中传递一个字。正如我前面所说的在几段中读
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct words
{
char unique[21];
int count;
} test;
void inputFile (words essay[100]);
int search (int index, int subscript, int integer,words essay[100]);
int main()
{
words essay[100];
inputFile (&test);
cout << essay[0].unique<<test.count;
return 0;
}
void inputFile (words essay[100])
{
char fileName[81];
char copyName[81];
cout << "What is the name of the input file? \n";
cin >> fileName;
ifstream infile;
ofstream outfile;
int count = 0;
char line [81];
int ch;
infile.open(fileName);
outfile.open(copyName);
while ((ch = infile.peek()) != EOF)
{ // while not end of file
infile.getline (line[81].unique, 81);
cout << "Copying: " << line << endl;
count++;
outfile << essay << endl;
}
cout << "There were " << count << " lines copied\n";
cout << endl;
// close both files
infile.close();
outfile.close();
};
/*int search (int index, int subscript, int integer, struct words essay[100])
{
string key;
key = test.unique;
int n;
n = test.count;
int i;
i = 0;
while (i < n && essay[i] != key)
i++;
if (i == n)
{
i = -1;
}
return i;
};*/
是否会编译代码?在第一眼看来它不应该... ...仅供参考,在文本中计算单词是一件容易的事情,很多答案都描述了如何做到这一点。 http://stackoverflow.com/questions/16867944/counting-occurrences-of-each-word-in-a-text-file – alexbuisson
你不必偷看。只需写'while(infile.getline(...)){...}'。顺便说一句'[81] .unique'没有任何意义;你想在那里做什么?无论如何,你应该将'line'定义为'std :: string',并使用'std :: getline(infile,line)'而不是'infile.getline(...)'。 –
为什么如果你明确地谈论C++字符串,包括cstring?使用'std :: string'(标题) –
Manu343726