apue学习笔记(第五章 标准I/O)

本章讲述标准I/O库ide

 

流和FILE对象函数

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

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

fwide函数可用于流的定向。根据mode参数的不一样值,fwide函数执行不一样的工做rest

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

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

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

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

 

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

对一个进程预约义了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+),在关闭该文件或程序结束时将自动删除。

相关文章
相关标签/搜索