本文介绍了C文本/二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C的stdin / stdout文件似乎总是处于文本模式。


有没有办法运行C程序以便这些(但特别是

stdout)是二进制模式吗?


(我正在用C语言包装一种不同的语言,这不是
想要文本和二进制文件的概念。但是如果我输出一个字符串,例如

" ONE\\\
TWO \ n",这将在stdout和常规之间表现不同

(二进制)文件。我的操作系统示例:


" \ n"文本模式下输出13,10;二进制模式下输出10

" \w"在文本模式下输出13,13,10;在二进制模式下输出13,10


(\ w是一个等同于\ r的新转义码解决方法很尴尬

(我永远不会停止\ n扩展到stdout的13,10)所以会很好

来避免它们)


-

谢谢,


Bartc

The stdin/stdout files of C seem to be always in Text mode.

Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?

(I''m in the process of wrapping a different language around C which doesn''t
want the concept of text and binary files. But if I output a string such as
"ONE\nTWO\n", this will behave differently between stdout and a regular
(binary) file. Examples on my OS:

"\n" Output 13,10 in text mode; 10 in binary mode
"\w" Output 13,13,10 in text mode; 13,10 in binary mode

(\w is a new escape code equivalent to \r\n). Workarounds will be awkward
(and I could never stop \n expanding to 13,10 for stdout) so would be nice
to avoid them)

--
Thanks,

Bartc

推荐答案



这就是标准所说的。


-

pete

That''s what the standards say.

--
pete




是的,请像这样使用freopen:


FILE * fin,* fout,* ferr;


fin = freopen(NULL," rb",stdin);

fout = freopen(NULL," ab",stdout);

ferr = freopen(NULL," ab",stderr);


您可以将返回值分配给stdin,stdout和stderr本身,

但是标准说他们不一定可以修改

lvalues。然而,它可能适用于您需要关注的大多数系统




详情请参阅标准7.19.5.4节。


< snip>

Yes, use freopen like this:

FILE *fin, *fout, *ferr;

fin = freopen(NULL, "rb", stdin);
fout = freopen(NULL, "ab", stdout);
ferr = freopen(NULL, "ab", stderr);

You could assign the return value to stdin, stdout and stderr itself,
but the standards says that they are not necessarily modifiable
lvalues. However it will probably work on most systems you would care
about.

See section 7.19.5.4 of the standard for details.

<snip>




是的,请像这样使用freopen:


FILE * fin,* fout,* ferr;


fin = freopen(NULL," rb",stdin);

fout = freopen(NULL," ab",stdout);

ferr = freopen(NULL," ab",stderr);


您可以将返回值分配给stdin,stdout和stderr本身,

但是标准说他们不一定可以修改

lvalues。然而,它可能适用于你会关心的大多数系统



Yes, use freopen like this:

FILE *fin, *fout, *ferr;

fin = freopen(NULL, "rb", stdin);
fout = freopen(NULL, "ab", stdout);
ferr = freopen(NULL, "ab", stderr);

You could assign the return value to stdin, stdout and stderr itself,
but the standards says that they are not necessarily modifiable
lvalues. However it will probably work on most systems you would care
about.



更重要的是,freopen不能保证做Bartc想要的事情。

因此,关键信息不是标准所说的,而是什么

典型的实现在文本和二进制模式之间存在差异的系统上进行。我只能给出一个数据点:

lcc-win32从freopen调用返回NULL(对于stdout)。


-

Ben。

More importantly, freopen is not guaranteed to do what Bartc wants.
Thus the key information is not what the standard says but what
typical implementations do on systems where there is difference
between text and binary mode. I can give only one data point:
lcc-win32 returns NULL from the freopen call (for stdout).

--
Ben.


这篇关于C文本/二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 08:17