换行符两次读取

时间:2015-09-21 20:44:53

标签: c++ char newline

我正在为我的一个类编写一个tokenizer,但是在阅读文件时我遇到了问题。

在这样的输入上,例如:

00 'this has a  
01 Return  
02 The end''s near

这是预期的输出:

(UNDEFINED,"'this has a  
Return  
The end''s near  
",1)  

但这是我得到的输出:

(UNDEFINED,"'this has a  
Return  
The end''s near  

",1)  

这是代码的一部分:

void readString(int& lineNumber, vector<Token*>& tokenList, ifstream& in)
{
char symbol;
int startingLineNumber = lineNumber;
string comment = "\'";
in.get(symbol);
do {

    switch (symbol) {
        case '\n':
            lineNumber++;
            comment += symbol;
            symbol = ' ';
            break;
        case '\'':
            if (in.peek() == '\'') {
                comment += symbol;
                in.get(symbol);
                comment += symbol;
            }
            else
            {
                comment += symbol;
                Token *newToken = NULL;
                newToken = new Token("STRING", comment, startingLineNumber);
                tokenList.push_back(newToken);
                return;
            }
            break;
        default:
            comment += symbol;
    }
} while (in.get(symbol));
readUndefined(comment, startingLineNumber, tokenList);
}

如果有人能帮助我弄清楚为什么会这样,我真的很感激。

谢谢

1 个答案:

答案 0 :(得分:0)

do...while错了! 您应该将do...while转换为while并检查first symbol不在循环中,或从循环中检查last symbolbreak