APUE函数笔记三: 标准IO库

原创 2012年03月24日 14:48:52

第五章  标准IO库:

stdin, stdout, stderr

#include <stdio.h>
#include <wchar.h>
int fwide(FILE * fp, int mode);
    we need clear error before call it,
    and check with ferror after call it

#include <stdio.h>
void setbuf(FILE * restrict fp, char * restrict buf);
    if buf is NULL, means close buffer, to be unbufferd
    else size of buf should >= BUFSIZ (BUFSIZ defined in stdio.h)
int setvbuf(FILE * restrict fp, char * restrict buf, int mode, size_t size);
    mode: _IOFBF, _IOLBF, _IONBF
    we'd better set buf as NULL to let function malloc suit size itself
    ret is 0 means success

#include <stdio.h>
int fflush(FILE * fp);
    if fp is NULL, fflush all ostream
    ret is 0 means success, or ret is EOF

#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 filedes, const char * type);
    type:
        r, rb; w, wb; a, ab; r+, rb+, r+b; w+, wb+, w+b; a+, ab+, a+b.

#include <stdio.h>
int fclose(FILE * fp);
    ret is 0 means success, or ret is EOF

#include <stdio.h>
int getc(FILE * fp);
    maybe it's a macro, may we cannot past getc as a argu, maybe it is faster
int fgetc(FILE * fp);
    it is safer, but maybe slowwer than getc, just maybe
int getchar();
    same as getc(stdin)

#include <stdio.h>
int ferror(FILE * fp);
    ret is 0 means False
int feof(FILE * fp);
    ret is 0 means False
void clearerr(FILE * fp);

#include <stdio.h>
int ungetc(int c, FILE * fp);
    ret == c means success, or ret is EOF

#include <stdio.h>
int putc(int c, FILE * fp);
    maybe it's a macro, may we cannot past putc as a argu, maybe it is faster
int fputc(int c, FILE * fp);
    it is safer, but maybe slowwer than putc, just maybe
int putchar(int c);
    same as putc(c, stdout);
    ret == c means success, or ret is EOF

#include <stdio.h>
char * fgets(char * restrict buf, int n, FILE * restrict fp);
    we need handle '\n' ourselves
char * gets(char * buf);
    buf maybe overflow, so gets is not safe, and it auto drop '\n' itself

#include <stdio.h>
int fputs(const char * restrict str, FILE * restrict fp);
    we need send '\n' to str,  if we need a new line
int puts(const char * str);
    safe, but it auto add a '\n' to the str tail itself
    ret is EOF means error

#include <stdio.h>
size_t fread(void * restrict ptr, size_t size, size_t nobj, 
             FILE * restrict fp);
    if ret < nobj, use ferror or feof to check it
size_t fwrite(const void * restrict ptr, size_t size, size_t nobj, 
              FILE * restrict fp);
    if ret < nobj, means error

#include <stdio.h>
long ftell(FILE * fp);
    ret is -1 means failed
long fseek(FILE * fp, long offset, int whence);
    whence: SEEK_SET, SEEK_CUR, SEEK_END
    SEEK_END may not implement in some OS, UNIX surpport it
    ret is 0 means success
void rewind(FILE * fp);

#include <stdio.h>
off_t ftello(FILE * fp);
    ret is -1 means failed
int fseeko(FILE * fp, off_t offset, int whence);
    ret is 0 means success

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

#include <stdio.h>
int printf(const char * restrict format, ...);
int fprintf(FILE * restrict fp, 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, ...);
    format: %[flags][fldwidth][precision][lenmodifier]convtype
        flags: -, +, (space), (#another-convtype), 0
        fldwidth: number or *
        precision: number or *
        lenmodifier: hh, h, l, ll, j, z, t, L
        convtype: d, i; o; u; x, X; a, A; e, E; f, f; g, G; 
                  c; s; p; n; %; C; S
    ret < 0, means failed

#include <stdarg.h>
#include <stdio.h>
int vprintf(const char * restrict format, va_list arg);
int vfprintf(FILE * restrict fp, const char * restrict format, va_list arg);
int vsprintf(char * restrict buf, const char * restrict format, va_list arg);
int vsnprintf(char * restrict buf, size_t n, const char * restrict format, 
              va_list arg);
    ret < 0, means failed

#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, ...);
    format: %[*][fldwidth][lenmodifier]convtype
        *: convert, but not update that element
        fldwidth: number
        lenmodifier: hh, h, l, ll, j, z, t, L
        convtype: d; i; o; u; x; a, A, e, E, f, F, g, G; 
                  c, s, [, [^, p, n, %, C; S
    ret is EOF, means failed

#include <stdarg.h>
#include <stdio.h>
int vscanf(const char * restrict format, va_list arg);
int vfscanf(FILE * restrict fp, const char * restrict format, va_list arg);
int vsscanf(const char * restrict buf, const char * restrict format, 
            va_list arg);
    ret is EOF, means failed

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

#include <stdio.h>
char * tmpnam(char * ptr);
    call TMP_MAX times at most (stdio.h)
    if ptr is NULL, name in a static block, means cannot call twice
    else size of ptr need >= L_tmpnam (stdio.h)
FILE * tmpfile(void);
    create a temp file, and auto unlink it soon (not close it)
    ret is NULL, means failed

#include <stdio.h>
char * tempnam(const char * directory, const char * prefix);
    if prefix not NULL, length of prefix need <= 5

#include <stdlib.h>
int mkstemp(char * template); 
    template like "fileXXXXXX", last 6 X will be replace by function
    this new file will not unlink itself
    ret is a file descriptor

示例:

#include <stdio.h>

int
main(int argc, char * argv[])
{
    int c;

    while ((c = getc(stdin)) != EOF) {
        if (putc(c, stdout) == EOF) {
            printf("output error\n");
            return -1;
        }
    }

    if (ferror(stdin)) {
        printf("input error\n");
        return -1;
    }

    return 0;
}

#include <stdio.h>

int
main(int argc, char * argv[])
{
    const int MAXLINE = 4096;
    char buf[MAXLINE];

    while (fgets(buf, MAXLINE, stdin) != NULL) {
        if (fputs(buf, stdout) == EOF) {
            printf("output error\n");
            return -1;
        }
    }

    if (ferror(stdin)) {
        printf("input error\n");
        return -1;
    }

    return 0;
}

#include <stdio.h>

void
pr_stdio(const char * name, FILE * fp)
{
    printf("stream = %s, ", name);

    if (fp->_IO_file_flags & _IO_UNBUFFERED) {
        printf("unbuffered");
    }
    else if (fp->_IO_file_flags & _IO_LINE_BUF) {
        printf("line buffered");
    }
    else {
        printf("fully buffered");
    }

    printf(", buffer size = %d\n", fp->_IO_buf_end - fp->_IO_buf_base);
}

int
main(int argc, char * argv[])
{
    FILE * fp;
    fputs("enter any character\n", stdout);
    if (getchar() == EOF) {
        printf("getchar error\n");
        return -1;
    }
    fputs("one line to standard error\n", stderr);

    pr_stdio("stdin", stdin);
    pr_stdio("stdout", stdout);
    pr_stdio("stderr", stderr);

    if ((fp = fopen("/etc/motd", "rb")) == NULL) {
        printf("fopen error\n");
        return -1;
    }
    if (fgetc(fp) == EOF && ferror(fp)) {
        printf("fgetc error\n");
        return -1;
    }
    pr_stdio("/etc/motd", fp);

    return 0;
}

#include <stdio.h>

int
main(int argc, char * argv[])
{
    FILE * fp;
    const int MAXLINE = 4096;
    char   line[MAXLINE];
    char   name[L_tmpnam];

    printf("%s\n", tmpnam(NULL));
    printf("%s\n", tmpnam(name));
    printf("%s\n", name);

    if ((fp = tmpfile()) == NULL) {
        printf("tmpfile error\n");
        return -1;
    }
    if (fputs("one line to output\n", fp) == EOF) {
        printf("fputs error\n");
        return -1;
    }
    rewind(fp);
    if (fgets(line, MAXLINE, fp) == NULL) {
        printf("fgets error\n");
        return -1;
    }
    if (fputs(line) == EOF) {
        printf("puts error\n");
        return -1;
    }

    return 0;
}

#include <stdio.h>

int
main(int argc, char * argv[])
{
    if (argc != 3) {
        printf("usage: ./a.out <directory> <prefix>\n");
        return 0;
    }
    printf("%s\n", tempnam(argv[1][0] != ' ' ? argv[1] : NULL, 
                           argv[2][0] != ' ' ? argv[2] : NULL));
    return 0;
}



相关文章推荐

读书笔记-APUE第三版-(5)标准IO库

ISO C标准I/O库使用流的概念读写文件。流是对数据传输的抽象,可以把流理解为从起点到终点间的字节序列。 标准I/O库通过维护进程空间内的缓冲区,减少read/write系统调用次数来提高I/O效率...
  • cargogo
  • cargogo
  • 2014年05月04日 21:24
  • 1002

标准IO库《APUE》 Chapter-5

介绍Linux中标准IO库,包含:流和FILE对象,标准输入输出和错误,各种类型缓冲,如何打开,读取,写入流。二进制IO的方法,如何在stream中定位。等等...

[APUE]再读之 标准IO库

本章介绍标准IO库 1. FILE对象 标准IO库一切函数基于FILE对象。FILE对象包含了管理该流的所有信息: 文件描述符,指向流缓存的指针,缓存的长度,缓存的字节数以及出错标识等 ...

APUE函数笔记一: 文件IO

第三章  文件IO: STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO filedes -> [0, OPEN_MAX], OPEN_MAX->63 #...

c语言文件IO库函数——APUE学习笔记(1)

大多数函数只需要五个函数实现IO操作:open, read, write, lseek, close. 一. 文件描述符: 文件描述符时一个非负整数(0~OPEN_MAX - 1).当打开现有文...

C++标准库笔记:13.12.4 以非格式化函数完成自定义IO操作符

自定义IO操作符时,我们可以使用stream内部对基本型别定义的operator来组合完成我们复杂的数据格式化;也可以使用IO非格式化函数(如read,get,write等stream的成员函数)来组...

Chapter5-标准I/O库(上)-「APUE读书笔记」

流和FILE对象 1、标准I/O库不同于一般的I/O函数,它们的操作是围绕流而不是文件描述符。当用标准I/O库打开或创建一个文件时,也就是让一个流与一个文件相关联。 2、标准I/O流可用于单字节或多...

Chapter5-标准I/O库(中)-「APUE读书笔记」

读和写流 当流打开后,有3种非格式化I/O可对其进行读写操作:(1)每次一个字符,(2)每次一行的I/O,(3)直接I/O。 1、输入函数 #include //三个函数返回取到的字符,出错或到达文...

《APUE》笔记-第五章-标准I/O库

#include FILE *fopen(const char *restrict pathname, const char *type); FILE *freopen(const char...

APUE学习笔记3——第五章——标准I/O库

APUE学习笔记3——第五章——标准I/O库
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:APUE函数笔记三: 标准IO库
举报原因:
原因补充:

(最多只允许输入30个字)