我确实有这段代码
// Open the file
infile.open(filename);
if (!infile) {
stockmsg << "InputData::read: Failed trying to open file " << filename << endl;
return NULL;
}
// Header line confirms if right kind of file
// In the future, might need to check the version number
count++;
infile >> keyword;
if (keyword != "#STOCK") {
stockmsg << "InputData::read : input file header line unrecognised" << endl;
return NULL;
}
getline(infile,nextline,'n'); // Discard the rest of the line
// Read the file line by line
etree = NULL;
while (status == READ_SUCCESS) {
count++;
// +++++++++++++++
// KEYWORDS stage
// +++++++++++++++
// When in KEY_WORDS mode, try to get another line
if (stage == KEY_WORDS) {
if (getline(infile,nextline,'n') == 0) {
stockmsg << "InputData::read : unexpected end of file at line " << count << endl;
status = READ_FAILURE;
break;
}
当我编译时,我收到一条错误消息
error: no match for 'operator=='
(operand types are 'std::basicistream<char>' and 'int')
if (getline(infile,nextline,'n')==0) {
我不知道如何解决这个问题。
它就是这么说的。
您执行了getline
,然后尝试将结果(是流(与0
进行比较。
这行不通。流和整数不能相互比较。然而,在流和布尔值之间有一种可以使用的魔力。
所以,写下这个:
if (!getline(infile,nextline,'n')) {
当流状态良好时,getline
表达式将是"truthy"。
(我在一定程度上简化了流的模糊性是如何工作的,但现在可以了。(