C语言之I/O处理函数

一、在Linux的文件系统调用中,所有函数都是根据文件描述符fd来访问文件的,如下:

1 文件描述符的创建和关闭

  • int open(const char *path, int oflag,...); //成功则返回文件描述符fd,出错则返回-1
  • int create(const char *path, mode_t mode); //成功则返回只写打开的文件描述符fd, 出错则返回-1;open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
  • int close(int fd); //成功则返回0,出错则返回-1;

2 文件的读写

  • ssize_t read(int fd, void *buf, size_t nbytes); //成功则返回读到的字节数,已到文件尾则返回0,出错则返回-1
  • ssize_t write(int fd, void *buf, size_t nbytes); //成功则返回已写的字节数,出错则返回-1

3 文件的定位

  • off_t lseek(int fd, off_t offset, int whence); //成功则返回文件偏移量,出错则返回-1;whence: SEEK_SET, SEEK_CUR, SEEK_END;

二、为了支持跨平台(Linux+Windows),一般使用C语言的标准I/O库,它是通过流来访问文件的,而且标准I/O库提供多种缓冲机制:

  • 全缓冲:只有填满了缓冲区后才进行实际的I/O操作,比如fread、fwrite等;
  • 行缓冲:遇到换行符后执行I/O操作,比如stdin、stdout;
  • 不带缓冲:遇到字符就立即执行I/O操作,比如stderr;

1 流的打开和关闭

  • FILE *fopen(const char *path, const char *type); //成功则返回文件指针,出错则返回NULL;type:r读,w写,a追加写,+读写,b二进制
  • int fclose(FILE *fp); //成功则返回0,出错则返回EOF;

2 读写一个字符的I/O流

  • int fgetc(FILE *fp); //成功则返回下一个字符,已到文件尾或出错则返回EOF;
  • int fputc(int c, FILE *fp) //成功则返回c,出错则返回EOF;
  • int getc(FILE *fp); //宏
  • int putc(int c, FILE *fp); //宏

3 读写一行的I/O流

  • char *fgets(char *buf, int n, FILE *fp); //成功则返回buf,已到文件尾或出错则返回EOF;读取一行或者n-1个字符,然后自动加上'\0';
  • int fputs(const char *buf, FILE *fp); //成功则返回非负值,出错则返回EOF;
  • char *gets(char *buf); //一直读取直到遇到'\n'或者EOF停止,最后自动把'\n'替换为'\0'
  • int puts(const char *buf);

4 直接I/O流:返回读写的对象数nobj

  • size_t fread(void *buf, size_t size, size_t nobj, FILE *fp);
  • size_t fwrite(void *buf, size_t size, size_t nobj, FILE *fp);

5 流的定位

  • long ftell(FILE *fp); //成功则返回当前文件位置指示,出错则返回-1
  • int fseek(FILE *fp, long offset, int whence); //成功则返回0,出错则返回-1,whence:SEEK_SET、SEEK_CUR、SEEK_END
  • void rewind(FILE *fp); //等同于fseek(fp, 0, SEEK_SET);

6 格式化I/O流

(1)格式化输出:成功则返回输出的字符数,出错则返回负值;

  • int printf(const char *format, ......); //终端stdout 
  • int fprintf(FILE *fp, const char *format, ......); //文件流
  • int dprintf(int fd, const char *format, ......); //文件描述符
  • int sprintf(char *buf, const char *format, ......); //数组buf
  • int snprintf(char *buf, size_t n, const char *format, ......); //带长度n的数组buf 

(2)变种的格式化输出:把可变参数......替换为arg,这是上面函数的具体实现

  • int vprintf(const char *format, va_list arg); //终端stdout 
  • int vfprintf(FILE *fp, const char *format, va_list arg); //文件流
  • int vdprintf(int fd, const char *format, va_list arg); //文件描述符
  • int vsprintf(char *buf, const char *format, va_list arg); //数组buf
  • int vsnprintf(char *buf, size_t n, const char *format, va_list arg); //带长度n的数组buf 

(3)格式化输入:成功则返回输入是项数,已到文件尾或出错则返回EOF;

  • int scanf(const char *format, ......); //终端stdin
  • int fscanf(FILE *fp, const char *format, ......); //文件流
  • int sscanf(char *buf, const char *format, ......); //数组buf

(4)变种的格式化输入:把可变参数......替换为arg,这是上面函数的具体实现

  • int vscanf(const char *format, va_list arg); //终端stdin
  • int vfscanf(FILE *fp, const char *format, va_list arg); //文件流
  • int vsscanf(char *buf, const char *format, va_list arg); //数组buf

printf系列输出格式总结:

  • %c:字符
  • %s:字符串
  • %f:浮点数(float或double)
  • %d:有符号的十进制整数(%ld:长整型)
  • %u:无符号的十进制整数(%lu:长整型,%zu:size_t类型
  • %l:长整型;%h:短整型
  • %o:八进制整数
  • %x:十六进制整数
  • %p:指针 
  • -:左对齐,默认是右对齐
  • 0:空位补0,默认是补空格
  • m.n:m表示域宽即输出项的宽度,n表示精度即小数位数(默认是6位)

比如int a=1234,%6d表示a=  1234,%06d表示a=001234,%-6d表示a=1234  ,%2d表示a=1234;

转载于:https://www.cnblogs.com/bo1990/p/11379783.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以使用标准库函数`freopen`来实现I/O重定向。`freopen`函数的原型如下: ```c FILE *freopen(const char *filename, const char *mode, FILE *stream); ``` 其中,`filename`参数是重定向后的文件名;`mode`参数是文件的打开模式,可以是`"r"`(只读)、`"w"`(只写)、`"a"`(追加)、`"r+"`(读写)或`"w+"`(读写,若文件存在则清空);`stream`参数是需要重定向的文件流,可以是`stdin`、`stdout`或`stderr`。 下面是一个简单的I/O重定向函数的实现,可以重定向输入、输出和错误流: ```c #include <stdio.h> void redirect_io(const char *input_file, const char *output_file, const char *error_file) { if (input_file != NULL) { freopen(input_file, "r", stdin); } if (output_file != NULL) { freopen(output_file, "w", stdout); } if (error_file != NULL) { freopen(error_file, "w", stderr); } } ``` 你可以在自己的程序中调用这个函数来实现I/O重定向,例如: ```c int main(int argc, char *argv[]) { redirect_io("input.txt", "output.txt", "error.txt"); printf("This will be redirected to output.txt\n"); fprintf(stderr, "This will be redirected to error.txt\n"); return 0; } ``` 这个程序会将标准输入重定向到`input.txt`文件,标准输出重定向到`output.txt`文件,标准错误输出重定向到`error.txt`文件。执行后,在`output.txt`文件中会输出`This will be redirected to output.txt`,在`error.txt`文件中会输出`This will be redirected to error.txt`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值