C ++ std :: istream readsome不读取任何内容 [英] C++ std::istream readsome doesn't read anything

查看:77
本文介绍了C ++ std :: istream readsome不读取任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像读书不读书一样.返回0,并且不读取任何字符.怎么了?

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

输出:

嗯,流看起来不错. 0:L

Well, stream looks good. 0 : L

推荐答案

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

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);

从流中提取最多n个字符,并将它们存储在s所指向的数组中,一旦关联的流缓冲区对象(如果有)保存的内部缓冲区用完字符时,立即停止 ,即使尚未到达文件结尾.

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.

这篇关于C ++ std :: istream readsome不读取任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆