gpt4 book ai didi

C++ std::istream readsome 不读取任何内容

转载 作者:可可西里 更新时间:2023-11-01 18:15:53 30 4
gpt4 key购买 nike

这就像 readsome 甚至没有阅读。返回 0 并且不读取任何字符。这里有什么问题?

#include <fstream>
#include <iostream>

int main ()
{
std::fstream stream("list.cpp", std::ios::in);

if (stream.good() || !stream.bad() || stream.is_open()) {

std::cout << "Well, stream looks good." << std::endl;

char justOneChar = 'L';
auto ssize = stream.readsome(&justOneChar, 1);

std::cout << ssize << " : " << justOneChar << std::endl;
}

return -1;
}

输出:

嗯,流看起来不错。0 : 大号

最佳答案

咨询a reference ,

The behavior of this function is highly implementation-specific. For example, when used with std::ifstream, some library implementations fill the underlying filebuf with data as soon as the file is opened (and readsome() on such implementations reads data, potentially, but not necessarily, the entire file), while other implementations only read from file when an actual input operation is requested (and readsome() issued after file opening never extracts any characters). Likewise, a call to std::cin.readsome() may return all pending unprocessed console input, or may always return zero and extract no characters.

简而言之,readsome 是相当无用的,至少对于标准流而言是这样。这是一个 super 非阻塞读取:它只会加载已在用户级进程中缓冲的数据,并且永远不会进行内核调用。

关于C++ std::istream readsome 不读取任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27098420/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com