《UNIX环境高级编程》五标准I/O库读书笔记

1、流和FILE对象
对于标准I/O库,它们的操作是围绕流(stream)进行的。当用标准I/O库打开或创建一个文件时,我们已使一个流与一个文件相关联。

fwide函数用于设置流的定向

#include <stdio.h>
#include <wchar.h>
int fwide(FILE *fp,int mode);
若流是宽定向的,返回正值,若流是字节定向的,返回负值,若流是为定向的,返回0

2、标准输入、标准输出和标准错误
这3个标准I/O流通过预定义文件指针stdin,stdout,stderr加以引用。

3、缓冲
全缓冲、行缓冲(换行符)、不带缓冲
标准错误是不带缓冲的,打开至终端设备的流是行缓冲的,其他流是全缓冲的。
在标准I/O库方面,flush(冲洗)意味着将缓冲区中的内容写到磁盘中

更改缓冲类型:

#include <stdio.h>
void setbuf(FILE *restrict fp,char *restrict buf);
int setvbuf(FILE *restrict fp,char *restrict buf,int mode,size_t size);
若成功,返回0;若出错,返回非0

  可以使用setbuf函数打开或关闭缓冲机制。为了带缓冲进行I/O,参数buf必须指向一个长度为BUFSIZ的缓冲区。为了关闭缓冲,将buf设置为NULL。

  使用setvbuf,可以精确地说明所需的缓冲类型。mode:
  _IOFBF 全缓冲
  _IOLBF 行缓冲
  _IONBF 不带缓冲
如果指定一个不带缓冲的流,则忽略buf和size参数。如果该流是带缓冲的,而buf是NULL,则标准I/O库将自动地为该流分配适当长度的缓冲区。

强制冲洗一个流:

#include <stdio.h>
int fflush(FILE *fp);
若成功,返回0;若出错,返回-1

此函数使该流所有未写的数据都被传送至内核。如若fp是NULL,则此函数将导致所有输出流被冲洗。

4、打开流

#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);
若成功,返回文件指针,若出错,返回NULL

freopen函数常用于将一个指定的文件打开为一个预定义的流:标准输入、标准输出或标准错误。
fdopen函数常用于由创建管道和网络通信通道函数返回的描述符。

tyoe参数指定对该I/O流的读、写方式:
r或rb 为读而打开
w或wb 把文件截断至0场,或为写而创建
a或ab 追加:为在文件尾写而打开,或为写而创建
r+或r+b或rb+ 为读和写而打开
w+或w+b或wb+ 把文件截断至0长,或为读和写而打开
a+或a+b或ab+ 为在文件尾读和写而打开或创建

关闭一个流:

#include <stdio.h>
int fclose(FILE *fp);
若成功,返回0;若出错,返回-1

5、读和写流
一旦打开了流,则可在3中不同类型的非格式化I/O中进行选择,对其进行读、写操作:
(1)每次一个字符的I/O
(2)每次一行的I/O
(3)直接I/O

输入函数:

#include <stdio.h>
int getc(FILE *fp);
int fgetc(FILE *fp);
int getchar(void); //等同于getc(stdin)
用于一次读一个字符,若成功,返回下一个字符,若已到达文件尾端或出错,返回EOF。

区分已到达文件尾端还是出错:

#include <stdio.h>
int ferror(FILE *fp);
int feof(FILE *fp);
若条件为真,返回非-;否则,返回0;

void clearerr(FILE *fp);清除FILE对象中出错标志和文件结束标志

从流中读取数据以后,可以调用ungetc将字符再压送回流中:

#include <stdio.h>
int ungetc(int c,FILE *fp);
若成功,返回c;若出错,返回EOF

输出函数:

#inlcude <stdio.h>
int putc(int c,FILE *fp);
int fputc(int c,FILE *fp);
int putchar(int c);
若成功,返回c;若出错,返回EOF;

6、每次一行I/O

#include <stdio.h>
char *fgets(char *restrict buf,int n,FILE *restrict fp);
若成功,返回buf;若已到达文件尾端或出错,返回NULL

对于fgets,必须指定缓冲的长度n。此函数一直读到下一个换行符为止,但是不超过n-1个字符。如若该行包括最后一个航航富的字符数超过n-1,则fgets只返回一个不完整的行,但是缓冲区总是以null字节结尾。对fgets的下一次调用会继续读该行。

#include <stdio.h>
in fputs(const chr *restrict str,FILE *restrict fp);
若成功,返回非负值;若出错,返回EOF

7、二进制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,FIEL *restrict fp);
返回读或写的对象数

8、定位流

#include <stdio.h>
long ftell(FILE *fp);
若成功,返回当前文件位置只是;若出错,返回-1L
int fseek(FILE *fp,long offset,int whence);
若成功,返回0;若出错,返回-1.
void rewind(FILE *fp);将一个流设置到文件的起始位置
#include <stdio.h>
off_t ftello(FILE *fp);
若成功,返回当前文件位置;若出错,返回off_t-1
int fseeko(FILE *fp,off_t offset,int whence);
若成功,返回0;若出错,返回-1.

fgetpos将文件位置指示器的当前值存入由pos指向的对象中。在以后调用fsetpos时,可以使用此值将流重新定位至该位置:

#include <stdio.h>
int fgetpos(FILE *restrict fp,fpos_t *restrict pos);
int fsetpos(FILE *fp,const fpos_t *pos);
若成功,返回0;若出错,返回非0

9、格式化I/O
格式化输出:

#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,...);
若成功,返回存入数组的字符数;若编码出错,返回负值

printf将格式化数据写到标准输出,fprintf写至指定的流,dprintf写至指定的文件,sprintf将格式化的字符送入数组buf中。

格式化输入:

#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,...);
返回赋值的输入项数;若输入出错或已到达文件尾端,返回EOF

10、实现细节

#include <stdio.h>
int fileno(FILE *fp);
返回与该流相关联的文件描述符

11、临时文件

#include <stdio.h>
char *tmpnam(char *ptr)
返回指向唯一路径名的指针
FILE *tmpfile(void);
若成功,返回文件指针;若出错,返回NULL
#include <stdlib.h>
char *mkdtemp(char *template);
若成功,返回指向目录名的指针,若出错,返回NULL
int mkstemp(char *template);
若成功,返回文件描述符,若出错,返回-1.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值