以二进制模式写入标准输出的最简单方法是什么? [英] What is the simplest way to write to stdout in binary mode?

查看:16
本文介绍了以二进制模式写入标准输出的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找出将二进制数据从 C 程序写入标准输出的最佳方法.它在 Linux 上运行良好,但在 Windows 上编译时遇到问题,因为 "被转换为 ".

I've been trying to figure out the best way to write binary data to stdout from a C program. It works fine on Linux, but I'm having issues when I compile on Windows because " " gets converted to " ".

是否有一种标准方法可以以某种二进制模式写入标准输出,从而避免换行符转换?如果没有,让 Windows 停止这样做的最简单方法是什么?

Is there a standard way to write to stdout in some sort of binary mode which avoids newline conversion? If not, what is the simplest way to get Windows to stop doing this?

我正在使用 GCC 和 MinGW,并使用 fwrite 写入 stdout.

I'm using GCC and MinGW, and writing to stdout using fwrite.

推荐答案

可以使用setmode(fileno(stdout), O_BINARY)

如果您想让它与 Linux 兼容,请将其包装在 ifdef 中.

Wrap it in an ifdef if you want to keep it compatible with Linux.

另请参阅:https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setmode?view=vs-2017

这篇关于以二进制模式写入标准输出的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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