C++ std::istream readsome doesn't read anythin

2019-04-25 01:27发布

It's like readsome isn't even reading. Returns 0 and doesn't read any chars. What is wrong here?

#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;
}

Output:

Well, stream looks good. 0 : L

3条回答
smile是对你的礼貌
2楼-- · 2019-04-25 01:34

It doesn't work because there might not be any characters available for reading at that instant. It works as a non-blocking read function as someone mentioned, therefore this means that it doesn't wait for input, which is why it wasn't asking you for input. So for you to use it to successfully read anything, there must be something available in the underlying buffer.

To see the number of characters that can be extracted in the next call to readsome, use the in_avail function.

查看更多
贼婆χ
3楼-- · 2019-04-25 01:37

Consulting 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.

In short, readsome is fairly useless, at least for standard streams. It's a super-nonblocking read: it will only load data that has been buffered in the user-level process, and it will not ever make a kernel call.

查看更多
Root(大扎)
4楼-- · 2019-04-25 01:56
auto ssize = stream.readsome(&justOneChar, 1);

1 is the maximum number of characters to read. If the streams internal buffers are empty when you call it, you'll get back zero as a return value.

The following quote (with my bold) shows this aspect:

streamsize readsome (char* s, streamsize n);

Extracts up to n characters from the stream and stores them in the array pointed by s, stopping as soon as the internal buffer kept by the associated stream buffer object (if any) runs out of characters, even if the end-of-file has not yet been reached.

The function is meant to be used to read data from certain types of asynchronous sources that may eventually wait for more characters, since it stops extracting characters as soon as the internal buffer is exhausted, avoiding potential delays.

It's basically a way to get as many characters as are available (subject to your specified limit) without having to wait for the stream to provide more.

查看更多
登录 后发表回答