从 std::cin 读取二进制数据

发布于 2024-12-06 18:18:28 字数 105 浏览 0 评论 0 原文

将二进制(非格式化)数据从 std::cin 读取到 stringstringstream 的最简单方法是什么?

What is the easiest way to read binary (non-formated) data from std::cin into either a string or a stringstream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小镇女孩 2024-12-13 18:18:28

std::cin 未使用 ios_binary 打开。如果您必须使用 cin,那么您需要重新打开它,这不是标准的一部分。

这里有一些想法: https://comp.unix.programmer.narkive.com/jeVj1j3I/how-can-i-reopen-std-cin-and-std-cout-in-binary-mode

一旦它是二进制的,您可以使用 cin.read() 来读取字节。如果您知道在您的系统中,文本和二进制之间没有区别(并且不需要可移植),那么您可以直接使用 read 而无需担心。

std::cin is not opened with ios_binary. If you must use cin, then you need to reopen it, which isn't part of the standard.

Some ideas here: https://comp.unix.programmer.narkive.com/jeVj1j3I/how-can-i-reopen-std-cin-and-std-cout-in-binary-mode

Once it's binary, you can use cin.read() to read bytes. If you know that in your system, there is no difference between text and binary (and you don't need to be portable), then you can just use read without worrying.

魄砕の薆 2024-12-13 18:18:28

对于 Windows,您可以将 _setmode 函数与 cin.read() 结合使用,如前所述。

_setmode(_fileno(stdin), _O_BINARY);
cin.read(...);

请参阅此处的解决方案源:http://talmai- oliveira.blogspot.com/2011/06/reading-binary-files-from-cin.html

For windows, you can use the _setmode function in conjunction with cin.read(), as already mentioned.

_setmode(_fileno(stdin), _O_BINARY);
cin.read(...);

See solution source here: http://talmai-oliveira.blogspot.com/2011/06/reading-binary-files-from-cin.html

望笑 2024-12-13 18:18:28

在 Unix/POSIX 系统上,您可以使用 cin.get() 方法逐字节读取数据并将数据保存到容器中,例如 std::vector ;,或者您可以使用 cin.read() 将固定数量的字节读入缓冲区。您还可以使用 cin.peek() 来检查任何数据流结束指示器。

请记住,避免对此类操作使用 operator>> 重载...只要观察到分隔符,使用 operator>> 就会导致发生中断,并且它还会从流本身中删除分隔字符。这将包括相当于空格、制表符等的任何二进制值。因此,您最终使用该方法从 std::cin 存储的二进制数据将与输入二进制流字节不匹配-字节。

On a Unix/POSIX system, you can use the cin.get() method to read byte-by-byte and save the data into a container like a std::vector<unsigned int>, or you can use cin.read() in order to read a fixed amount of bytes into a buffer. You could also use cin.peek() to check for any end-of-data-stream indicators.

Keep in mind to avoid using the operator>> overload for this type of operation ... using operator>> will cause breaks to occur whenever a delimiter character is observed, and it will also remove the delimiting character from the stream itself. This would include any binary values that are equivalent to a space, tab, etc. Thus the binary data your end up storing from std::cin using that method will not match the input binary stream byte-for-byte.

且行且努力 2024-12-13 18:18:28

所有预定义的 iostream 对象都必须绑定到相应的 C 流:

对象 cin 控制来自与对象 stdin 关联的流缓冲区的输入,该对象在 中声明。

http://eel.is/c++draft/narrow.stream.objects< /a>

因此获取二进制数据的方法与C相同:

基本上,您真正能做的最好的事情就是:

freopen(NULL, "rb", stdin);

这将重新打开标准输入以成为相同的输入流,但以二进制形式
模式。在正常模式下,从 Windows 上的 stdin 读取将转换为
\r\n(Windows 换行符)转换为单个字符 ASCII 10。使用
“rb”模式禁用此转换,以便您可以正确读入
二进制数据。

https://stackoverflow.com/a/1599093/6049796

All predefined iostream objects are obligated to be bound to corresponding C streams:

The object cin controls input from a stream buffer associated with the object stdin, declared in <cstdio>.

http://eel.is/c++draft/narrow.stream.objects

and thus the method of obtaining binary data is same as for C:

Basically, the best you can really do is this:

freopen(NULL, "rb", stdin);

This will reopen stdin to be the same input stream, but in binary
mode. In the normal mode, reading from stdin on Windows will convert
\r\n (Windows newline) to the single character ASCII 10. Using the
"rb" mode disables this conversion so that you can properly read in
binary data.

https://stackoverflow.com/a/1599093/6049796

明月夜 2024-12-13 18:18:28

cin.read 将存储固定数量的字节,没有任何逻辑搜索 @Jason 提到的类型的分隔符。

然而,流上可能仍然存在活动的翻译,例如 CRLF -> 。 NL,所以它仍然不是二进制数据的理想选择。

cin.read would store a fixed number of bytes, without any logic searching for delimiters of the type that @Jason mentioned.

However, there may still be translations active on the stream, such as CRLF -> NL, so it still isn't ideal for binary data.

贵在坚持 2024-12-13 18:18:28

cplusplus.com

  • 未格式化的输入

    istream的大多数其他成员函数
    类用于执行未格式化的输入,即没有解释
    根据输入的字符进行制作。这些成员函数可以
    从输入字符中获取确定数量的字符
    序列(获取getline, 查看阅读readsome)...

正如 Lou Franco 指出的那样, std::cin 不打开std::ios_base::binary,但其中一个函数可能会让您接近您正在寻找的行为。

cplusplus.com:

  • Unformatted input

    Most of the other member functions of the istream
    class are used to perform unformatted input, i.e. no interpretation is
    made on the characters got form the input. These member functions can
    get a determined number of characters from the input character
    sequence (get, getline, peek, read, readsome)...

As Lou Franco pointed out, std::cin isn't opened with std::ios_base::binary, but one of those functions might get you close to the behavior you're looking for.

十年不长 2024-12-13 18:18:28

使用 windows/mingw/msys/bash,如果需要通过管道传输不同的命令并在其间插入二进制流,则需要将 std::cin 和 std::cout 作为二进制流进行操作。

Mikhail 的 _setmode 解决方案运行良好。

使用 MinGW,需要的标头如下:

#include <io.h>
#include <fcntl.h> 

With windows/mingw/msys/bash, if you need to pipe different commands with binary streams in between, you need to manipulate std::cin and std::cout as binary streams.

The _setmode solution from Mikhail works perfectly.

Using MinGW, the neaded headers are the following:

#include <io.h>
#include <fcntl.h> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文