为什么fstream::ignore()能用,而fstream::sync()不能用?

问题描述 投票:0回答:1

你好,我是一个C++新手,我想知道下面的代码有什么问题。

int MAX = 15;
while (!inputfile.eof()){
    getline(inputfile, name);
    cout << name << endl;
    for (int i = 0; i < MAX; i ++){
        inputfile >> questionnumber >> answer;
        cout << questionnumber << "  " << answer << "\n";
    }
    //inputfile.ignore(); #This will ignore the Enter key in Buffer zone
    //inputfile.sync(); #But how come this can't clean the Enter key in Buffer zone?
}

输入文件中的数据是

key
 2 B
13 D
 3 C
 5 D
14 C
 6 C
 8 A
15 B
 9 A
 1 A
 7 B
10 B
11 C
 4 D
12 D
 0 James

对不起,我的英语不好,不知道怎么描述这种问题。

谢谢。

c++ getline
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.