Ifstream - 重置 EOF 位

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

我正在与

ifstream
合作。我一直读到
EOF
位被设置(我需要这样)。

为什么这不起作用?

// IN is ifstream file. CH is char.

if (IN.eof()) {
    IN.seekg(ios::beg);
    IN.clear();

    if (read((char*)&CH, sizeof(CH)))
        cout << "Success.";
    else
        cout << "Not S.";    
}

read
功能永远不会成功。我尝试使用
IN.setstate(ifstream::goodbit)
而不是
IN.clear()
。但还是失败了,对吗?

c++ ifstream eof
1个回答
7
投票

像这样更改你的代码:

IN.clear();
IN.seekg(0, ios::beg);
© www.soinside.com 2019 - 2024. All rights reserved.