标准编程c [英] Programming in standard c

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

问题描述

在我的圣诞快乐中消息,我提出了一个函数来读取
a文件到RAM缓冲区并返回该缓冲区或NULL如果

文件不存在或发现其他一些错误。


有趣的是看到该消息的答案证明

完全用标准C编程完全不可能

对于像我提议的那样一个小而荒谬的简单程序。


1我以二进制模式读取文件内容,应该允许我

使用ftell / fseek确定文件大小。


没有人提出异议,当然除了明显的

之一,如果是文件的话。是一些与stdin相关的文件,对于某些unix机器/ dev / tty01或类似的
实例...


我没有测试这个,因为它在标准C中是不可能的:

isatty()不在标准中。


2)没有可移植的方法来确定哪些字符应该是。一个

读卡器(CB Falconer)建议以二进制模式打开文件

然后在文本模式下比较两个缓冲区以查看哪个

个字符丢失了...好吧,那太贵了。


3)我为POSIX定义的errno使用了不同的值,但没有使用

C标准,只定义了几个。再一次,错误处理

并不是一个重要的标准化,根据委员会的b $ b。 errno在那里,但它的使用绝对是

根本不可移动,并且立即超出标准C

提供的。


我们一遍又一遍地听到这个小组是关于标准C *ONLY*。

那么有人可以告诉我这个简单的程序是如何用标准写的
C?


这证实了我关于需要提高标准库质量

的论点!


你不能只用标准的C做*任何*。

-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http:/ /www.cs.virginia.edu/~lcc-win32

In my "Happy Christmas" message, I proposed a function to read
a file into a RAM buffer and return that buffer or NULL if
the file doesn''t exist or some other error is found.

It is interesting to see that the answers to that message prove that
programming exclusively in standard C is completely impossible even
for a small and ridiculously simple program like the one I proposed.

1 I read the file contents in binary mode, what should allow me
to use ftell/fseek to determine the file size.

No objections to this were raised, except of course the obvious
one, if the "file" was some file associated with stdin, for
instance under some unix machine /dev/tty01 or similar...

I did not test for this since it is impossible in standard C:
isatty() is not in the standard.

2) There is NO portable way to determine which characters should be
ignored when transforming a binary file into a text file. One
reader (CB Falconer) proposed to open the file in binary mode
and then in text mode and compare the two buffers to see which
characters were missing... Well, that would be too expensive.

3) I used different values for errno defined by POSIX, but not by
the C standard, that defines only a few. Again, error handling
is not something important to be standardized, according to
the committee. errno is there but its usage is absolutely
not portable at all and goes immediately beyond what standard C
offers.

We hear again and again that this group is about standard C *"ONLY"*.
Could someone here then, tell me how this simple program could be
written in standard C?

This confirms my arguments about the need to improve the quality
of the standard library!

You can''t do *anything* in just standard C.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32

推荐答案

在文章< fk ******* ***@aioe.org> ;, jacob navia< ja *** @ nospam.orgwrote:
In article <fk**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:

>在我的Happy Christmas&q中UOT;消息,我提出了一个函数来读取一个文件到RAM缓冲区并返回该缓冲区,如果文件不存在或者发现其他错误,则返回NULL。
>In my "Happy Christmas" message, I proposed a function to read
a file into a RAM buffer and return that buffer or NULL if
the file doesn''t exist or some other error is found.


> 1我以二进制模式读取文件内容,应该允许我

使用ftell / fseek来确定文件大小。
>1 I read the file contents in binary mode, what should allow me
to use ftell/fseek to determine the file size.


没有人提出异议,除了明显的

之一,如果是文件的话。是一些与stdin关联的文件,对于某些unix机器/ dev / tty01或类似的
实例...
No objections to this were raised, except of course the obvious
one, if the "file" was some file associated with stdin, for
instance under some unix machine /dev/tty01 or similar...



二进制文件可能读取有一个不确定数量的额外二元数

0'在他们的最后。

-

"历史是一堆碎片" - Laurie Anderson

Binary files may read with an indeterminate number of extra binary
0''s at the end of them.
--
"History is a pile of debris" -- Laurie Anderson


文章< fk ********** @ aioe.org> ;, jacob navia< ja *** @nospam .orgwrote:
In article <fk**********@aioe.org>, jacob navia <ja***@nospam.orgwrote:

>在我的圣诞节快乐中消息,我提出了一个函数来读取一个文件到RAM缓冲区并返回该缓冲区,如果文件不存在或者发现其他错误,则返回NULL。
>In my "Happy Christmas" message, I proposed a function to read
a file into a RAM buffer and return that buffer or NULL if
the file doesn''t exist or some other error is found.


> 2)在将二进制文件转换为二进制文件时,没有可移植的方法来确定应该忽略哪些字符?
一个文本文件。一个

读卡器(CB Falconer)建议以二进制模式打开文件

然后在文本模式下比较两个缓冲区以查看哪个

个字符丢失......好吧,那太贵了。
>2) There is NO portable way to determine which characters should be
ignored when transforming a binary file into a text file. One
reader (CB Falconer) proposed to open the file in binary mode
and then in text mode and compare the two buffers to see which
characters were missing... Well, that would be too expensive.



tmpnam()获取临时文件名;在一种模式下写一些简短的文件

,在另一种模式下freopen()文件(确保

你不要破坏内容),然后看看读完了什么。

-

那时候我还很年轻,但我也很朦胧。

- Christopher Priest

tmpnam() to get a temporary file name; write something short to the file
in one mode, freopen() the file in the other mode (making sure
you don''t clobber the contents), then see what is read in.
--
"I was very young in those days, but I was also rather dim."
-- Christopher Priest


jacob navia写道:
jacob navia wrote:

在我的圣诞节快乐中消息,我提出了一个函数来读取
a文件到RAM缓冲区并返回该缓冲区或NULL如果

文件不存在或发现其他一些错误。


有趣的是看到该消息的答案证明

完全用标准C编程完全不可能

对于像我提议的那样一个小而荒谬的简单程序。
In my "Happy Christmas" message, I proposed a function to read
a file into a RAM buffer and return that buffer or NULL if
the file doesn''t exist or some other error is found.

It is interesting to see that the answers to that message prove that
programming exclusively in standard C is completely impossible even
for a small and ridiculously simple program like the one I proposed.



确定可能已在文本模式下打开的流所需的缓冲区大小的最便携方式是:使用

重复调用fread()直到文件结束,

跟踪读取的总字节数,并重新分配为

你去。当然,它效率低下;但对于
对应于设备而不是文件的流,实际上没有

替代品。即使对于与实际文件相对应的流,也有
是真正的操作系统,其中没有更有效的方法来查找文件的

长度。如果你不关心那些

平台的可移植性,你没有理由不能使用特定于操作系统的技术

来确定文件长度。


如果您使用这种方法,则无需担心任何特定实施的终止线是什么?b $ b,无论是CR, LF,

CRLF,LFCR,或者用空字符填充行到

a固定的最大行大小,或者实现的任何其他方法

找到ocnvev

The most portable way to determine the size of the buffer needed to
store a stream that might have been opened in text mode is to use
repeated calls to fread() until the end of file has been reached,
keeping track of the total number of bytes read, and reallocating as
you go along. Sure, it''s inefficient; but for streams which
correspond to devices, rather than files, there''s really no
alternative. Even for streams which correspond to actual files, there
are real OSs where there''s no more efficient method of finding the
length of the file. If you don''t care about portability to those
platforms, there''s no reason why you can''t use OS-specific techniques
for determining the file length.

If you use that approach, there''s no need to worry about how lines are
terminated for any particular implementation, whether with CR, LF,
CRLF, LFCR, or by padding the lines with null characters to the end of
a fixed maximum line size, or any other method that an implementation
finds ocnvev


3)我为POSIX定义的errno使用了不同的值,但没有使用
3) I used different values for errno defined by POSIX, but not by



我认为没有理由,在任何时候,你的程序应该将errno设置为

除了基础C标准库

函数已经具有的任何值之外的任何值把它设置为。如果fopen()还没有设置

ENOENT,为什么要strfromfile()?如果fseek()还没有设置EIO,

为什么要strfromfile()?如果malloc()还没有设置ENOMEM,为什么

应该strfromfile()?不要让事情变得比以前更难;

即使strfromfile()是标准化的,我仍然会建议

实现者捎带掉它调用的低级函数,而不是设置它自己的错误代码。

I saw no reason why, at any point, your program should set errno to
any value other than whichever one the underlying C standard library
functions would have already set it to. If fopen() doesn''t already set
ENOENT, why should strfromfile()? If fseek() doesn''t already set EIO,
why should strfromfile()? If malloc() doesn''t already set ENOMEM, why
should strfromfile()? Don''t make things harder than they have to be;
even if strfromfile() were standardized, I''d still recommend that the
implementor piggy-back off the lower level functions it calls, rather
than setting it''s own error codes.


这篇关于标准编程c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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