zoukankan      html  css  js  c++  java
  • apue学习笔记(第五章 标准I/O)

    本章讲述标准I/O库

    流和FILE对象

    对于标准I/O库,它们的操作是围绕流进行的。流的定向决定了所读、写的字符是单字节还是多字节的。

    #include <stdio.h>
    #include <wchar.h>
    int fwide(FILE *fp,int mode);

    fwide函数可用于流的定向。根据mode参数的不同值,fwide函数执行不同的工作

    若mode参数值为负,fwide将试图使指定的流是字节定向的

    若mode参数值为正,fwide将试图使指定的流是宽定向的

    若mode参数值是0,fwide将不试图设置流的定向,但返回标志该流定向的值

    当一个流最初被创建时,它并没有定向。若在未定向的流上使用一个多字节(单字节)I/O函数,则将该流设置为宽(字节)定向。fwide并不改变已定向流的定向。

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

    对一个进程预定义了3个流:stdin、stdout、stderr。

    缓冲

    标准I/O提供了以下3种类型的缓冲

    1 全缓冲,在这种情况下,在填满标准I/O缓冲区后才进行实际I/O操作。如磁盘上的文件通常实施全缓冲。

    2 行缓冲,在这种情况下,当在输入和输出中遇到换行符,标准I/O执行I/O操作。如标准输入输出。

    3 不带缓冲,标准I/O库不对字符进行缓冲存储。标准错误流stderr通常是不带缓冲的。

    我们可以调用下面两个函数更改缓冲类型

    #include <stdio.h>
    void setbuf(FILE *restrict fp,char *restrict buf);
    int setvbuf(FILE *restrict fp,char *buf,int mode,size_t size);

    可以使用setbuf打开或关闭缓冲机制,参数buf指定一个长度为BUFSIZ的缓冲区。将buf设置为NULL可以关闭缓冲。

    使用setvbuf,我们可以精确地说明所需的缓冲类型。这是用mode参数实现的:

    _IOFBF  全缓冲

    _IOLBF  行缓冲

    _IONBF  不带缓冲

    我们可以通过fflush函数冲洗一个流

    #include <stdio.h>
    int fflush(FILE *fp);

    打开流

    下面3个函数打开一个标准I/O流

    #include <stdio.h>
    FILE *fopen(const char *restrict pathname,const char *restrict type);
    FILE *freopen(const char *restrict pathname,const char *restrict type,FILE *restrict fp);
    FILE *fdopen(int fd,const char *type);

    type参数指定对该I/O流的读、写方式,ISO C规定type参数可以有如下15种不同的值

    其中b作为type的一部分,这使得标准I/O系统可以区分文本文件和二进制文件

    调用fclose关闭一个打开的流

    #include <stdio.h>
    int fclose(FILE *fp);

    读和写流

    以下3个函数可用于一次读一个字符

    #include <stdio.h>
    int getc(FILE *fp);
    int fgetc(FILE *fp);
    int getchar(void);   //等同于getc(stdin)

    对应上面所述的每个输入函数都有一个输出函数

    #include <stdio.h>
    int putc(int c,FILE *fp);
    int fputc(int c,FILE *fp);
    int putchar(int c);

    每次一行I/O

    下面两个函数提供每次输入一行的功能

    #include <stdio.h>
    char *fgets(char *restrict buf,int n,FILE *restrict fp); //buf为缓冲区地址,读入的行将送入其中,参数n指定缓冲的长度
    char *gets(char *buf);  //不推荐使用

    fputs和puts提供每次输出一行的功能

    #include <stdio.h>
    int fputs(const char *restrict str,FILE *restrict fp);
    int puts(const char *str);

    二进制I/O

    下列两个函数执行二进制I/O操作

    #include <stdio.h>
    size_t fread(void *restrict ptr,size_t size,size_t nobj,FILE *restrict fp);
    size_t fwrite(const void *restrict ptr,size_t size,size_t nobj,FILE *restrict fp);

    参数size为欲写结构的长度,nobj为欲写的元素个数,函数返回的是读或写的对象数。这些函数有以下两种常见的用法

    float data[10];
    if(fwrite(&data[2],sizeof(float),4,fp)!=4)
        err_sys("fwrite error");
    //读写一个结构
    struct{
          short count;
          long total;
          char name[NAMESIZE];  
    }item;
    
    if(fwrite(&item,sizeof(item),1,fp)!=1)
         err_sys("fwrite error");

    定位流

    有3种方法定位标准I/O流

    #include <stdio.h>
    long ftell(FILE *fp);   //若成功,则返回当前文件位置指示,出错则返回-lL
    int fseek(FILE *fp,long offset,int whence);  
    void rewind(FILE *fp);

    除了偏移量的类型是off_t而非long以外,ftello函数与ftell相同,fseeko函数与fseek相同

    #include <stdio.h>
    off_t ftello(FILE *fp);
    int fseeko(FILE *fp,off_t offset,int whence);

    下面函数是ISO C标准引入的

    #include <stdio.h>
    int fgetpos(FILE *restrict fp,fpos_t *restrict pos);
    int fsetpos(FILE *fp,const fpos_t *pos);

    格式化I/O

    格式化输出是由printf函数处理的

    #include <stdio.h>
    int printf(const char *restrict format,...);
    int fprintf(FILE *restrict fp,const char *restrict format,...);
    int dprintf(int fd,const char *restrict format,...);
    
    int sprintf(char *restrict buf,const char *restrict format,...);
    int snprintf(char *restrict buf,size_t n,const char *restrict format,...);    //参数n指定缓冲区长度

    执行格式化输入处理的是3个scanf函数

    #include <stdio.h>
    int scanf(const char *restrict format,...);
    int fscanf(FILE *restrict fp,const char *restrict format,...);
    int sscanf(const char *restrict buf,const char *restrict format,...);

    临时文件

    ISO C标准I/O库提供了两个函数以帮助创建临时文件

    #include <stdio.h>
    char *tmpnam(char *ptr);
    FILE *tmpfile(void);

    tmpnam函数产生一个与现有文件名不同的一个有效路径名字符串

    tmpfile创建一个临时二进制文件(类型wb+),在关闭该文件或程序结束时将自动删除。

  • 相关阅读:
    计算机图形学和OpenGL(二)坐标系和绘制点线函数
    计算机图形学和OpenGL(一)OpenGL初步
    C++ 实现链表常用功能
    Cocos2d-x环境搭建
    2014年学习计划
    2013年终总结
    AS3开发必须掌握的内容
    starling性能优化
    后补个2012年的总结吧
    原生javascript实现图片懒加载
  • 原文地址:https://www.cnblogs.com/runnyu/p/4635382.html
Copyright © 2011-2022 走看看