APUE学习:标准I/O库

流与FILE对象

1.流的定向:
决定了所读,所写的字符是单字节还是多字节字符集。

#include <stdio.h>
#include <wchar.h>
int fwide(FILE *fp, int mode); //设置流的定向

缓冲

1.全缓冲:在填满标准I/O缓冲区之后才进行实际I/O操作。例如写到文件中时会使用全缓冲。
2.行缓冲:在输入与输出中遇到换行符,标准I/O库执行I/O操作。当流涉及到一个终端(例如标准输入和标准输出),通常使用行缓冲。
3.不带缓冲

#include <stdio.h>

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

这里写图片描述

打开流

#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, cosnt char *type);

这里写图片描述

读和写流

1)字符读写

#include <stdio.h>
int getc(FILE *fp);  //int putc(int c, FILE *fp);  //int fgetc(FILE* fp);
int fputc(int c, FILE *fp);

int getchar(void); 
int putchar(int c); 

int ferror(FILE *fp);

int feof(FILE *fp);

void clearerr(FILE *fp);

2)可以用下列函数把字符送回流

int ungetc(int c, FILE *fp);

一次只能回送一个字符,回送的字符不必一定是上一次读到的字符,不可以回送EOF。但是到达文件尾端时,任然可以回送一个字符。下次读将返回该字符,再次读则返回EOF。
因为ungetc会清除流的文件结束符。

3)一次读写一行

#include <stdio.h>

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

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

定位流

格式化I/O

杂记

#include <stdio.h>
int fileno(FILE *fp);  //返回与流fp有关的文件的文件描述符
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值