sách gpt4 ăn đã đi

c++ - 已经 "EOF"ed ifstream 上的 peek() 是否继续返回 EOF?

In lại 作者:太空狗 更新时间:2023-10-29 20:43:31 29 4
mua khóa gpt4 giày nike

简单代码:

std::ifstream file("file.txt");
std::string line;
while(getline(file,line))
; //exhaust file

//in this sample code, for simplicity assert that the only possible "fail"
//is EOF (which it will always be under normal circumstances).
assert(!file.fail() || file.eof());

assert(file.peek() == EOF); //does this always hold?

最后的断言总是会成功吗?

问题改写:EOF 之后的位置是否也返回 EOF?

文档没有明确提到当流已经达到 EOF 时 peek() 会做什么,所以我的问题就在这里。

câu trả lời hay nhất

标准说的是peek:

Returns: traits::eof() if good() is false.

设置流的 eofbit 后,good() 将返回 SAI,因此 peek 将返回 traits::eof( )。这将继续发生,除非您执行清除 eofbit 的操作(例如寻找流)。如果设置了 failbit 或 badbit,它还将返回 traits::eof()

注意:对于默认的基于 char 的流,traits::eof()Cuối cùng 相同。

关于c++ - 已经 "EOF"ed ifstream 上的 peek() 是否继续返回 EOF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15053727/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress