ITeye博客

UNIX编程(5)-标准IO库

brxonline · 2011-06-27 16:55 · C · 操作系统 · Markdown

1.流的定向 freopen函数清除一个流的定向,fwide设置流的定向

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

int fwide(FILE *fp, int mode); 如果mode的参数值为负,fwide将试图使指定的流是字节定向的。 如果mode的参数值为正,fwide将试图使指定的流是宽定向的。 如果mode的参数值为0,fwide将不试图设置流的定向, 但返回标识该流定向的值。 2.缓冲 标准IO提供了三种类型的缓冲: 全缓冲 行缓冲 不带缓冲

有两个函数可以更改缓冲类型

#include <stdio.h>

void setbuf(FILE *restrict fp, char *restrict buf);

int setvbuf(FILE *restrict fp, char *restrict buf, int mode, size_t size);

任何时候我们都可以强制冲洗一个流 #include <stdio.h>

int fflush(FILE *fp);

3.打开流

#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参数指定对IO流的读、写方式

r or rb 为读而打开

w or wb 把文件截短至0,或为写而打开

a or ab 添加,为在文件尾写而打开,或为写而创建

r+ or r+b or rb+ 为读和写而打开

w+ or w+b or wb+ 把文件截短至0长,或为读和写而打开

a+ or a+b or ab+ 为在文件尾读和写而打开或创建

4.读写流 一次读一个字符 #include <stdio.h>

int getc(FILE *fp);

int fgetc(FILE *fp);

int getchar(void);

不管是出错还是到达文件尾端,这三个函数都返回同样的值。为了区分这两种情况必须用下面两个函数 #include <stdio.h>

int ferror(FILE *fp);

int feof(FILE *fp);

清除标志 void clearerr(FILE *fp);

从流中读取数据后,可以调用ungetc将字符再压送回流中 #include <stdio.h>

int ungetc(int c, FILE *fp);

每次输出一个字符的函数 #include <stdio.h>

int putc(int c, FILE *fp);

int fputc(int c, FILE *fp);

int putchar(int c);

每次一行函数 #include <stdio.h>

char *fgets(char *restrict buf, int n, FILE *restrict fp);

char *gets(char *buf);

#include <stdio.h>

int fputs(const char *restrict str, FILE *restrict fp);

int puts(const char *str);

二进制读写

#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);

5.定位流

#include <stdio.h>

long ftell(FILE *fp);

Returns: current file position indicator if OK, 1L on error

int fseek(FILE *fp, long offset, int whence);

Returns: 0 if OK, nonzero on error

void rewind(FILE *fp);

#include <stdio.h>

off_t ftello(FILE *fp);

Returns: current file position indicator if OK, (off_t)1 on error

int fseeko(FILE *fp, off_t offset, int whence);

#include <stdio.h>

int fgetpos(FILE *restrict fp, fpos_t *restrict pos);

int fsetpos(FILE *fp, const fpos_t *pos);

6.格式化输出和输入 格式化输出 #include <stdio.h>

int printf(const char *restrict format, ...);

int fprintf(FILE *restrict fp, const char *restrict format, ...);

format说明

%convtype

四种格式函数变体 #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);

Both return: number of characters output if OK, negative value if output error

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);

Both return: number of characters output if OK, negative value if output error

int sprintf(char *restrict buf, const char *restrict format, ...);

int snprintf(char *restrict buf, size_t n, const char *restrict format, ...);

格式化输入

#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, ...); #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);

7.临时文件 两个函数帮助创建临时文件

#include <stdio.h>

char *tmpnam(char *ptr);

Returns: pointer to unique pathname

FILE *tmpfile(void);

例:

#include "apue.h"

int main(void) { char name, line; FILE *fp;

printf("%s\n", tmpnam(NULL));       /* first temp name */

tmpnam(name);                       /* second temp name */
printf("%s\n", name);

if ((fp = tmpfile()) == NULL)       /* create temp file */
    err_sys("tmpfile error");
fputs("one line of output\n", fp);  /* write to temp file */
rewind(fp);                         /* then read it back */
if (fgets(line, sizeof(line), fp) == NULL)
    err_sys("fgets error");
fputs(line, stdout);                /* print the line we wrote */

exit(0);

}

#include <stdio.h>

char *tempnam(const char *directory, const char *prefix);

#include <stdlib.h>

int mkstemp(char *template);

例: #include "apue.h"

int main(int argc, char *argv) { if (argc != 3) err_quit("usage: a.out ");

printf("%s\n", tempnam(argv != ' ' ? argv : NULL,
  argv != ' ' ?  argv : NULL));

exit(0);

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。