读取至'n'(行尾)

Reading until ' ' (End of Line)

本文关键字:行尾 读取      更新时间:2023-10-16

首先,我是C++的初学者,要有商业头脑。我找不到答案。

嗨,我正在试着写一个翻译。当我尝试这个时,我选择"\n"作为行终止符:

#define __TEST__ 1
while(Source >> Word){ //Source is file descriptor. I can't use EOF method because of if i do that, i will need to write two statments. Critical...
    if(Word == 'n'){ // Word is a string object.
        //Clean the vector
#       if __TEST__
            cout << "Succesful!" << "n";
#       endif       
    }               
}

当我试图编译此代码时,由于"标记,它会给出一个错误。当我用"标记更改它时,编译器不会给出错误,但在运行时,程序无法检测到行尾。解决这个问题的最快方法是什么?

>>丢弃空白,因此Word永远不会包含换行符。

如果您想一直读到行的末尾,您应该使用getline函数而不是>>