unix环境高级编程-5.2~3标准I/O库。

5.1引言

I/O对于操作系统是一个非常重要的。无论是各种类型的数据库。

5.2 流和FILE对象

第三章,我们可以回忆到I/O函数都是文件描述符的。即是打开一个文件,返回一个文件描述符。然后对文件描述符进行后续的I/O操作。

而对于标准的I/O库,操作则围绕流进行的!当用标准I/O库打开或创建一个文件的时候,我们已经是用一个流与一个文件相关联。标准io文件流可用于单字节和多字节字符集。流的定向决定了所读,写的字符是个单字节还是多字节的。

当一个流最初被创建的时候,他并没有定向。如若在未定向的流上是用一个多字节的I/O函数,则将该留定向设置为宽定西。如果在未定向的流上使用一个单字节I/O函数,则将该留的定向设置为字节定西。只有两个函数可以改变流的定向。

freopen函数,清楚一个流的定向;fwide函数设置流的定向。

#include<stdio.h>
#include<wchar.h>

int fwide(FILE *fp,int mode);


返回值:如果流是宽定向的则返回正值,若流是字节定向的则返回负值,或者流是未定向的则返回0;

查看GNU c

int fwide (FILE *stream, int mode) [Function]
The fwide function can be used to set and query the state of the orientation of the
stream stream. If the mode parameter has a positive value the streams get wide
oriented, for negative values narrow oriented. It is not possible to overwrite previous
orientations with fwide. I.e., if the stream stream was already oriented before the
call nothing is done.
If mode is zero the current orientation state is queried and nothing is changed.
The fwide function returns a negative value, zero, or a positive value if the stream is
narrow, not at all, or wide oriented respectively.
This function was introduced in Amendment 1 to ISO C90 and is declared in
‘wchar.h’.
It is generally a good idea to orient a stream as early as possible. This can prevent
surprise especially for the standard streams stdin, stdout, and stderr. If some library
function in some situations uses one of these streams and this use orients the stream in a
different way the rest of the application expects it one might end up with hard to reproduce
errors. Remember that no errors are signal if the streams are used incorrectly. Leaving
a stream unoriented after creation is normally only necessary for library functions which
create streams which can be used in different contexts.
When writing code which uses streams and which can be used in different contexts it
is important to query the orientation of the stream before using it (unless the rules of the
library interface demand a specific orientation). The following little, silly function illustrates
this.

 

void
print_f (FILE *fp)
{
if (fwide (fp, 0) > 0)
/* Positive return value means wide orientation. */
fputwc (L’f’, fp);
else
fputc (’f’, fp);
}

Note that in this case the function print_f decides about the orientation of the stream
if it was unoriented before (will not happen if the advise above is followed).
The encoding used for the wchar_t values is unspecified and the user must not make any
assumptions about it. For I/O of wchar_t values this means that it is impossible to write
these values directly to the stream. This is not what follows from the ISO C locale model
either. What happens instead is that the bytes read from or written to the underlying media
are first converted into the internal encoding chosen by the implementation for wchar_t.
The external encoding is determined by the LC_CTYPE category of the current locale or by

the ‘ccs’ part of the mode specification given to fopen, fopen64, freopen, or freopen64.
How and when the conversion happens is unspecified and it happens invisible to the user.
Since a stream is created in the unoriented state it has at that point no conversion
associated with it. The conversion which will be used is determined by the LC_CTYPE
category selected at the time the stream is oriented. If the locales are changed at the runtime
this might produce surprising results unless one pays attention. This is just another good
reason to orient the stream explicitly as soon as possible, perhaps with a call to fwide.

从以上可以知道:

  • 如果mode的参数值是负值,fwide将试图使指定的流是字节定向的。
  • 如果mode参数值为正的,fwide将使指定的流是宽定向的。
  • 如果mode参数值为0,fwid将不试图设置流的定向,但返回表示该流定向的值

注意fwide并不改名已经定向的流的定向。还应该注意的是,fwide是无出错返回的。试想如果流是无效的,那么将发生什么呢?我们唯一可以依靠的是,掉欧诺个fwide前腰先清除errno,从fwide返回是检查errno的值,

当打开一个流的时候,标准I/O函数fopen费那会以一个FILE的对象指针。该对象通常是一个结构,它包含了标准的I/O库位管理该流所需要的所有信息,包括用于实际I/O的文件描述符。指向用于该流缓冲区的指针,缓冲区的长度,当前在缓冲区中的字符数,以及出错标志。等等。

为了引用一个流,要将FILE指针作为参数传递给每个标准的I/O函数,我们称指向FILE对象的指针为文件指针。

5.3标准输入,标准输出和标准出错

对一个进程预定义三个六,并且三个流被进程自动使用,他们是标准输入,标准输出和标准出错,

这三个标准IO流通过预订文件指针stdin,stdout和stderr加以引用,都定义在stdio.h

 

 

更多内容欢迎访问http://blog.csdn.net/wallwind

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值