【发布时间】:2021-02-28 02:53:37
【问题描述】:
我不断收到一条错误消息,在 getline(Input, sentence) 上显示“错误:没有用于调用 'getline(std::ifstream&, char [100])'| 的匹配函数”。我可能会遗漏一些东西,我不确定。我要做的是从输入中读取整行并使用 getline 将其插入 char sentence[100] 中。我必须根据作业的要求使用 getline。提前谢谢你。
char uniqueWords[100][16] = {""};
int multipleWords = 0;
int theWords[100];
int totalWords = 0;
char sentence[100];
char delimit[] = " /.";
ifstream Input;
Input.open("input.txt");
if (!Input.is_open())
{
cout << "Error Opening File";
exit(EXIT_FAILURE);
}
ofstream Output;
Output.open("output.txt", std::ios_base::app);
if (!Output.is_open())
{
cout << "Error Opening File";
exit(EXIT_FAILURE);
}
char *p;
while(!Input.eof())
{
getline(Input, sentence);
p = strtok(sentence, delimit);
while(p)
{
Output << p ;
words(p, uniqueWords, theWords, multipleWords); // a function for the assignment
totalWords++;
p = strtok(nullptr,delimit);
}
create << "." << endl;
}
【问题讨论】:
-
当然。
getline (stream, std::string)将std::string作为参数。您想要stream.getline (char array, size)(例如Input.getline (sentence, sizeof sentence))为什么要使用字符数组而不是std::string和std::vector<std::string>>进行存储?另见Why !.eof() inside a loop condition is always wrong.
标签: c++