的std :: istream的:: readsome [英] std::istream::readsome

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

问题描述



我想要做的是从流中读取字节,任何数字和任何

时间。我希望它等到有任何字节要读。


我想要与cstdio'的'fread'完全相同的功能但是在一个

std :: istream。


最初出现的是readome。我会做我想要的但是

看起来根本不能很好地工作(至少用gcc!)。当从gcc上的命名管道读取
时,它立即返回 - 没有错误,

只是立即返回,是预期的行为吗?


What I would like to do is read bytes from a stream, any number and any
time. I would like it to wait until there are any bytes to read.

I want the exact same functionality as cstdio''s "fread" but in a
std::istream.

It appeared at first that "readsome" would do exactly what I wanted but
it appears not to work very well at all (at least with gcc!). When
reading from a named pipe on gcc, it returns immediately - no errors,
just immediate return, is that the expected behaviour ?

推荐答案

Hello Gianni!

Gianni Mariani写道:
Hello Gianni!
Gianni Mariani wrote:

我是什么我想做的是从流中读取字节,任何数字和任何

时间。我希望它等到有任何字节要读。
What I would like to do is read bytes from a stream, any number and any
time. I would like it to wait until there are any bytes to read.



没有这样的功能 - 在C库中没有C ++

库。

At至少,这些库不需要做这样的事情。

There is no such functionality - neither in the C library no in the C++
library.
At least, these libraries are not required to do anything like this.


我想要与cstdio''fread"完全相同的功能。但是在一个

std :: istream中。
I want the exact same functionality as cstdio''s "fread" but in a
std::istream.



您可以使用std :: istream :: read()。但是,这将阻止

直到

到达输入流的任一端或者收到所请求的

字符数 - 就像当然是fread()的情况。如果你的系统上的
fread()返回的内容与上述不同,那么

错误或你没有在文件上使用它。在后一种情况下,



依赖于系统特定功能和C或C ++标准

对行为没有要求无论如何调用。

You can use std::istream::read() for this. This will, however, block
until
either end of the input stream is reached or the requested number of
characters is received - as is the case for fread(), of course. If the
fread() on your system returns differently than described above, it is
either wrong or you are not using it on files. In the latter situation,
you
are relying on system specific features and the C or C++ standards
make no requirements on the behavior of the calls whatsoever.


首先出现的是readome。会做我想要的事情
It appeared at first that "readsome" would do exactly what I wanted



这是不太可能的:readome()的默认实现只是

获得缓冲区中的字符底层流

缓冲区。

如果没有,它只会返回,表明它没有读取

characers。

默认实现永远不会尝试读取任何字节。从std :: streambuf派生的类b / b
可以选择使用不同的方法,但

标准流缓冲区都没有。

This is rather unlikely: the default implementation of readsome() just
obtains characters which are in the buffer of the underlying stream
buffer.
If there are none, it just returns, indicating that it read no
characers. The
default implementation does never attempt to read any bytes. Classes
derived from std::streambuf can choose to use a different approach but
none of the standard stream buffers does.


当从gcc上的命名管道读取
时,它会立即返回 - 没有错误,

只是立即返回,这是预期的行为吗?
When
reading from a named pipe on gcc, it returns immediately - no errors,
just immediate return, is that the expected behaviour ?



是:没有字符,即无事可做。你可能

想要检查gcount()中读取的字符数。


祝你好运,Denise。

Yes: there are no characters, i.e. there is nothing to do. You might
want to check gcount() for the number of characters read.

Good luck, Denise.


Denise Kleingeist写道:
Denise Kleingeist wrote:

Hello Gianni!

Gianni Mariani写道:
Hello Gianni!
Gianni Mariani wrote:

>我想要做的是从流中读取字节,任何数字和任何
时间。我希望它等到有任何字节要读。
>What I would like to do is read bytes from a stream, any number and any
time. I would like it to wait until there are any bytes to read.



没有这样的功能 - 在C ++中没有C ++

库。


There is no such functionality - neither in the C library no in the C++
library.



我不同意。 std :: fread完全符合我的要求。

I beg to differ. std::fread does exactly what I want.


至少,这些库不需要做这样的事情。
At least, these libraries are not required to do anything like this.



自25年前我写了我的

第一个C程序以来,这些库一直表现得很好。我不知道你在谈论什么。

These libraries have been performing exactly this way since I wrote my
first C program over 25 years ago. I have no idea what you talk about.


>
>

>我想要与cstdio''s" fread"完全相同的功能但是在
std :: istream中。
>I want the exact same functionality as cstdio''s "fread" but in a
std::istream.



您可以使用std :: istream :: read()。但是,这将阻止

直到

到达输入流的任一端或者收到所请求的

字符数 - 就像fread()的情况,


You can use std::istream::read() for this. This will, however, block
until
either end of the input stream is reached or the requested number of
characters is received - as is the case for fread(),



不幸的是,情况并非如此,对std :: istream :: read的调用将是

如果没有足够的数据来满足读取请求,则会失败。对于istream :: read调用接口,没有办法说明它已经读取了比所请求的字节数少的b $ b,因此最后一个块读取将是

几乎总是不可读。


当然。如果您系统上的

Unfortunately, this is not the case, a call to std::istream::read will
fail if there is not enough data to fulfill the read request. There is
no way for the istream::read call interface to indicate that it has read
fewer than the requested number of bytes so the last block read will
almost always be unreadable.

of course. If the


fread()的回复与上述不同,则为
fread() on your system returns differently than described above, it is



std: :fread确实按照我的意愿运作。返回最后一个块

的大小。我想用std :: istream做同样的事情。


.... snip

std::fread does operate exactly as I want. The size of the last block
being read is returned. I want to do the same thing with std::istream.

.... snip


>首先出现的是readome。我会做我想要的事情
>It appeared at first that "readsome" would do exactly what I wanted



这是不太可能的:只读取readome()的默认实现

获取缓冲区中的字符底层流

缓冲区。

如果没有,它只会返回,表明它没有读取

characers。

默认实现永远不会尝试读取任何字节。从std :: streambuf派生的类b / b
可以选择使用不同的方法,但

标准流缓冲区都没有。


This is rather unlikely: the default implementation of readsome() just
obtains characters which are in the buffer of the underlying stream
buffer.
If there are none, it just returns, indicating that it read no
characers. The
default implementation does never attempt to read any bytes. Classes
derived from std::streambuf can choose to use a different approach but
none of the standard stream buffers does.



这很奇怪,因为我使用的至少2个编译器与你说的完全相反

,对常规文件不小于。

That''s strange since at least 2 compilers I use do exactly the opposite
of what you say, on regular files no less.


>
>

>当从gcc上的命名管道读取时,它立即返回 - 没有错误,
只是立即返回,是预期的行为?
>When
reading from a named pipe on gcc, it returns immediately - no errors,
just immediate return, is that the expected behaviour ?



是:没有字符,即无事可做。您可能想要检查gcount()中读取的字符数。


Yes: there are no characters, i.e. there is nothing to do. You might
want to check gcount() for the number of characters read.



好​​的 - 你已经肯定了为什么要远离C ++ io的iostreams。它不仅是我无法弄清楚如何正确使用它的人。

OK - you have affirmed why keep away from iostreams for C++ io. It''s
not only I who can''t figure out how to use it properly.




Gianni Mariani在留言中写道......

Gianni Mariani wrote in message ...

>
首先出现的是readome。我会做我想要的但是
看起来根本不能很好地工作
>
It appeared at first that "readsome" would do exactly what I wanted but
it appears not to work very well at all



是的,知道你的意思。我以前是readome,然后是codesome。它总是

这个git完成的代码组!


-

鲍勃< ; GR

POVrookie

Yeah, know what you mean. I used to readsome, then codesome. It was always
the codesome that would "Git ''er done!".

--
Bob <GR
POVrookie


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

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