我不断收到一条错误消息,说“错误:在getline(Input,句子)上没有调用'getline(std :: ifstream&,char [100])'|”的匹配函数。我可能不确定,我不确定。我想做的是从输入中读取整行,并使用getline将其插入char句子[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;
}
问题是您正在尝试使用std :: getline而不是std :: basic_istream :: getline。(例如,您使用的是错误的getline()
)getline()
您正在尝试将期望值astd::string
用于目标存储,而不是普通的旧字符数组。上面的第二种形式很高兴地采用一个字符数组及其大小作为目标存储。您可以使用:
while (Input.getline(sentence, sizeof sentence))
{
p = strtok (sentence, delimit);
while (p)
{
Output << p ;
words (p, uniqueWords, theWords, multipleWords); // a function for the assignment
totalWords++;
p = strtok(nullptr,delimit);
}
std::cout << ".\n";
}
其他想法,看看为什么循环条件内的!.eof()总是错误的。。请注意,使用流状态控制读取循环,该状态可以从输入函数中返回。另请参阅为什么“使用命名空间标准”;被认为是不好的做法?。
输出错误消息时,请使用std::cerr
写入stderr
而不是stdout
。例如:
std::ifstream Input ("input.txt");
if (!Input.is_open())
{
std::cerr << "Error Opening File.\n";
exit (EXIT_FAILURE);
}
除此之外,您应该真正将其std::string
用于字符串存储和std::vector<std::string>
字符串集合的存储。您可以将每个字符串复制到一个字符数组中,以使用进行解析strtok()
。仔细看一下,如果您还有其他问题,请与我联系。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句