UNIX - 标准I/O库

本文详细介绍了标准I/O库中的流与FILE对象的概念,解释了流的定向及其改变方法,并提供了实例代码展示如何设置流的定向。此外,文章还概述了标准输入、标准输出和标准出错的预定义流程用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

流和FILE对象

对于标准I/O库,其操作是针对流(stream)进行的。当用标准I/O库打开或创建一个文件时,既已使一个流与一个文件相关联。

流的定向(stream’s orientation)
流的定向决定了所读、写的字符是单字节还是多字节的。当一个流最初被创建时,它未被定向。如果在一个未定向的流上使用一个单字节I/O函数,则将流的定向被设置为字节定向的。如果在一个未定向的流上使用一个多字节I/O函数,则将流的定向被设置为宽定向(多字节)的。

只有两个函数能够改变流的定向。freopen函数清除一个流的定向;fwide函数设置流的定向。

#include <wchar.h>
int fwide(FILE *stream, int mode);
返回值:若流是宽定向的则返回正值,若流是字节定向的则返回负值,若流是未定向的则返回0.

fwide函数由通过mode参数决定流的定向:

  • 若 mode > 0,将流设置为宽定向的;
  • 若 mode < 0,将流设置为字节定向的;
  • 若 mode = 0,不设置流的定向,但是返回标识该流定向的值。

例1. 使用fwide设置流的定向

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

int main(int argc, char *argv[])
{
    FILE *fp;
    int wd = 0;
    int wide = 0;

    if (argc != 3){
        printf("Usage: %s <path> <wide>\n", argv[0]);
        exit(1);
    }

    fp = fopen(argv[1], "r");
    if (!fp){
        printf("Open stream failed\n");
        exit(-1);
    }

    wide = atoi(argv[2]);
    wd = fwide(fp, wide);
    if (wd > 0)
        printf("%d : stream is wide-character oriented.\n", wd);
    else if (wd < 0)
        printf("%d : stream is byte oriented.\n", wd);
    else 
        printf("%d : stream has no orientation yet.\n", wd);

    fclose(fp);

    return 0;
}

将流设置为字节定向的:

$ ./a.out file -3
-1 : stream is byte oriented.

将流设置为宽定向的:

$ ./a.out file 3
1 : stream is wide-character oriented.

获取流的定向:

$ ./a.out file 0
0 : stream has no orientation yet.

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

FILE结构体定义:
[ /usr/include/libio.h ]

struct _IO_FILE {
    int _flags;     /* High-order word is _IO_MAGIC; rest is flags. */
    #define _IO_file_flags _flags
    /* The following pointers correspond to the C++ streambuf protocol. */
    /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
    char* _IO_read_ptr; /* Current read pointer */
    char* _IO_read_end; /* End of get area. */
    char* _IO_read_base;    /* Start of putback+get area. */
    char* _IO_write_base;   /* Start of put area. */
    char* _IO_write_ptr;    /* Current put pointer. */
    char* _IO_write_end;    /* End of put area. */
    char* _IO_buf_base; /* Start of reserve area. */
    char* _IO_buf_end;  /* End of reserve area. */
    /* The following fields are used to support backing up and undo. */
    char *_IO_save_base; /* Pointer to start of non-current get area. */
    char *_IO_backup_base;  /* Pointer to first valid character of backup area */
    char *_IO_save_end; /* Pointer to end of non-current get area. */

    struct _IO_marker *_markers;

    struct _IO_FILE *_chain;

    int _fileno;

    #if 0
        int _blksize;
    #else
        int _flags2;
    #endif
        _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

    #define __HAVE_COLUMN /* temporary */
    /* 1+column number of pbase(); 0 is unknown. */
    unsigned short _cur_column;
    signed char _vtable_offset;
    char _shortbuf[1];

    /*  char* _save_gptr;  char* _save_egptr; */

    _IO_lock_t *_lock;
    #ifdef _IO_USE_OLD_IO_FILE
};

[ /usr/include/stdio.h ]

__BEGIN_NAMESPACE_STD
/* The opaque type of streams.  This is the definition used elsewhere.  */
typedef struct _IO_FILE FILE;
__END_NAMESPACE_STD

标准输入、标准输出和标准出错

对一个进程预定义了三个流,且这三个流能自动被进程使用,它们是标准输入、标准输出和标准出错。
这三个标准I/O流通过预定义文件指针stdin、stdout和stderr加以引用。这三个指针同样在stdio.h中定义。

/* Standard streams.  */
extern struct _IO_FILE *stdin;          /* Standard input stream. */
extern struct _IO_FILE *stdout;         /* Standard output stream. */
extern struct _IO_FILE *stderr;         /* Standard error output stream. */
/* C89/C99 say they're macros.  Make them happy.  */
#define stdin stdin
#define stdout stdout
#define stderr stderr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值