zoukankan      html  css  js  c++  java
  • APUE学习笔记 Chapter 5 Standard I/O Library

     1.FILE对象
    FILE对象包含文件描述符,指向缓冲池的指针,缓冲的大小,已经读入的字节数,错误标志,EOF标志等等。


    2.缓冲
    缓冲的目的是尽可能地减少read和write的调用次数,提升IO系统的性能。
    总共有三种缓冲方式:
        2.1 全缓冲,fully buffered.文件将全量进入缓冲,这是大多数使用的缓冲方式。
        2.2 行缓冲,line buffered.通常使用在终端的输入输出。
        2.3 不缓冲,unbuffered. 不使用缓冲。
    ISO C中有如下要求:
        标准输入与输出不指向交互设备时,必须使用全缓冲。错误输出无论什么情况下都不使用全缓冲。

    可以使用以下两个函数进行缓冲的设置:setbuf(3),setvbuf(3).
    可以使用fflush(3)对缓冲进行flush操作,将缓冲的内容写入对应的文件。

    3.打开文件
     使用以下函数进行文件流的打开: fopen(3) , freopen(3) , fdopen(3)
     在使用文件流的时候有如下的限制,input不能直接出现在output之后,output也不能直接出现在input之后,除非有调用相关函数改变了当前的文件偏移量。
                   

    type

    Description

    r or rb

    open for reading

    w or wb

    truncate to 0 length or create for writing

    a or ab

    append; open for writing at end of file, or create for writing

    r+ or r+b or rb+

    open for reading and writing

    w+ or w+b or wb+

    truncate to 0 length or create for reading and writing

    a+ or a+b or ab+

    open or create for reading and writing at end of file


    4.关闭文件
      要使用fclose(3)来关闭一个文件流。

    5.读/写文件流
     对于读或写文件流,都有三种操作方式,每种操作方式都有相应的函数对应
        5.1.读一个字节:
               getc(3) , fgetc(3) , getchar(3).其中getc多数以marco形式实现,而fgetc保证是一个函数,可以操作函数指针,但是因为多了函数调用的开销,调用速度稍逊于getc。getchar默认是使用stdin的。
           也可以使用ungetc(3)将一个字节退回到未读状态。
       5.2.写一个字节:
              putc(3),fputc(3),putchar(3)用法与读一个字节的相似。
       5.3.读一行:
             fgets(3),gets(3),getc默认从stdin中读入。
       5.4.写一行:
             fputs(3),puts(3)
              对于系统性能而言,由于标准IO库具有缓冲,系统调用时间与read和write整块文件相差不大。
        5.5.读/写整块文件
              fread(3),fwrite(3),主要的作用为读出/写入一个数组或者一个结构, 但由于不同系统与编译器的实现不同,在真实操作中会出现问题。 
    6.在文件流中移动
              ftell(3)和fseek(3),rewind(3)回到文件头
    7.格式化输入输出:
              printf(3)和fprintf(3),sprintf(3)和snprintf(3)
                             基本格式:
                                  
                    %[flags][fldwidth][precision][lenmodifier]convtype
               flags:

    -

    left-justify the output in the field

    +

    always display sign of a signed conversion

    (space)

    prefix by a space if no sign is generated

    #

    convert using alternate form (include 0x prefix for hex format, for example)

    0

    prefix with leading zeros instead of padding with spaces

                        fld最小长度,不够的由空格补全
                        precision: 表示精度
                        lenmodifier:长度表示
               

    Length modifier

    Description

    hh

    signed or unsigned char

    h

    signed or unsigned short

    l

    signed or unsigned long or wide character

    ll

    signed or unsigned long long

    j

    intmax_t or uintmax_t

    z

    size_t

    t

    ptrdiff_t

    L

    long double


                        convtype:

    Conversion type

    Description

    d,i

    signed decimal

    o

    unsigned octal

    u

    unsigned decimal

    x,X

    unsigned hexadecimal

    f,F

    double floating-point number

    e,E

    double floating-point number in exponential format

    g,G

    interpreted as f, F, e, or E, depending on value converted

    a,A

    double floating-point number in hexadecimal exponential format

    c

    character (with l length modifier, wide character)

    s

    string (with l length modifier, wide character string)

    p

    pointer to a void

    n

    pointer to a signed integer into which is written the number of characters written so far

    %

    a % character

    C

    wide character (an XSI extension, equivalent to lc)

    S

    wide character string (an XSI extension, equivalent to ls)

    标准格式输出:
    scanf(3) , fscanf(3) , sscanf(3)
    8.标准I/O库的实现细节均在<stdio.h>中,可以从中获得相应的一些细节信息,包括缓冲情况,缓冲大小等。
     
    9.建立临时文件:
    可以使用以下两个函数:tmpnam(3),tmpfile(3),tempnam(3),mkstemp(3).
    通常使用临时文件的流程是先获得临时文件的路径名,然后创建它,然后马上unlink它。由于文件已经被unlink,所以当我
    们一close这个文件流时,系统将会为我们删除这个文件,达到临时文件的目的。而调用tmpfile打开的文件,系统会自动为我们删除它。
    由于tmpfile与tempnam都有一段的时间间隙,通常情况下,应该使用mkstemp来创建文件,并unlink它,然后再使用。

    10.几个提升buffer性能的库,主要是减少标准I/O库中的内存拷贝。有时间的话,值得研究一下
    sfio , ASI , uClibc C library , newlibc C library
  • 相关阅读:
    iOS 新建xib文件时,最外层view的约束问题
    React native 无法弹出调试控件的问题
    从GitHub下载demo时遇到的依赖问题
    Mac 解决 Sourcetree 同步代码总需要密码的问题
    Mac 安装JRE 1.8
    正则表达式-- (.*?) 或 (.*+)
    字符串内有多个#号,每俩#号为一组,JavaScript 截取每组#号之间的字符
    Js/jQuery实时监听input输入框值变化
    Redis设置密码
    redis本机能访问 远程不能访问的问题
  • 原文地址:https://www.cnblogs.com/liangxing/p/1802827.html
Copyright © 2011-2022 走看看