5.7 fgets/fputs和gets/puts-流的单行读写
2013-11-06 23:55
162 查看
fgets/fputs、gets/puts是两对用于流的单行读写函数。
函数原型:
返回值:若成功则返回buf,若已到达文件结尾或者出错则返回NULL
参数:
buf 为存放取出字符的缓冲区地址
n 为缓冲区大小
fp 文件指针
返回值:若成功则返回非负值,若出错则返回EOF
参数:
str 为存放输出字符的缓冲区地址
fp 文件指针
实例 x.5.7.1.c
编译与执行:
[root@localhost unixc]# echo "123456789" > /tmp/myfile
[root@localhost unixc]# cat /tmp/myfile
123456789
[root@localhost unixc]# cc x.5.7.1.c
[root@localhost unixc]# ./a.out
1234
[root@localhost unixc]#
分析:实例中虽然文件有9个字符,但是给定的缓冲区长度是5,扣除最后一个null结尾字符,只能取到4个字符。
实例 x.5.7.2.c
编译与执行:
[root@localhost unixc]# echo "abcdefghij" > /tmp/myfile
[root@localhost unixc]# cat /tmp/myfile
abcdefghij
[root@localhost unixc]# cc x.5.7.2.c
[root@localhost unixc]# ./a.out
123456789
[root@localhost unixc]# cat /tmp/myfile
1234[root@localhost unixc]#
分析:实例中尽管输入了9个数字,但是由于给定的缓冲区长度为5,扣除一个null结束标志,只能取到4个数字。
两个例子说明,更多时候应当选择使用fgets/fputs,因为这对输入输出有明确的缓冲区长度限制,从而避免缓冲区溢出导致不可预测的错误。
函数原型:
#include <stdio.h> char *fputs(char *restrict buf, int n, FILE *restrict fp); char *gets(char *buf);
返回值:若成功则返回buf,若已到达文件结尾或者出错则返回NULL
参数:
buf 为存放取出字符的缓冲区地址
n 为缓冲区大小
fp 文件指针
#include <stdio.h> int fputs(const char *restrict str, FILE *restrict fp); int puts(const char *str);
返回值:若成功则返回非负值,若出错则返回EOF
参数:
str 为存放输出字符的缓冲区地址
fp 文件指针
实例 x.5.7.1.c
#include <stdio.h> #define LINELEN 5 int main(void) { char pathname[] = "/tmp/myfile"; FILE *fp; char str[LINELEN]; if ((fp=fopen(pathname, "r")) == NULL) { /*为读打开流*/ printf("fopen error for %s\n", pathname); } else { fgets(str, LINELEN, fp); /*从流中读取一行*/ printf("%s\n", str); fclose(fp); /*关闭流*/ } return 0; }
编译与执行:
[root@localhost unixc]# echo "123456789" > /tmp/myfile
[root@localhost unixc]# cat /tmp/myfile
123456789
[root@localhost unixc]# cc x.5.7.1.c
[root@localhost unixc]# ./a.out
1234
[root@localhost unixc]#
分析:实例中虽然文件有9个字符,但是给定的缓冲区长度是5,扣除最后一个null结尾字符,只能取到4个字符。
实例 x.5.7.2.c
#include <stdio.h> #define LINELEN 5 int main(void) { char pathname[] = "/tmp/myfile"; FILE *fp; char str[LINELEN]; if ((fp=fopen(pathname, "w")) == NULL) { /*为写打开流,并将文件截短为0*/ printf("fopen error for %s\n", pathname); } else { fgets(str, LINELEN, stdin); /*从标准输入中读取一行*/ fputs(str, fp);/*将取得字符写入文件流*/ fflush(fp); /*冲刷流*/ fclose(fp); /*关闭流*/ } return 0; }
编译与执行:
[root@localhost unixc]# echo "abcdefghij" > /tmp/myfile
[root@localhost unixc]# cat /tmp/myfile
abcdefghij
[root@localhost unixc]# cc x.5.7.2.c
[root@localhost unixc]# ./a.out
123456789
[root@localhost unixc]# cat /tmp/myfile
1234[root@localhost unixc]#
分析:实例中尽管输入了9个数字,但是由于给定的缓冲区长度为5,扣除一个null结束标志,只能取到4个数字。
两个例子说明,更多时候应当选择使用fgets/fputs,因为这对输入输出有明确的缓冲区长度限制,从而避免缓冲区溢出导致不可预测的错误。
相关文章推荐
- 字符串读写函数gets() 、puts()、fgets()和fputs()
- fgets gets fputs puts
- fgets,fputs,gets,puts
- 字符串输入gets()、fgets()和字符串输出puts()、fputs()函数
- gets()fgets()scanf()和puts()fputs()printf()
- 字符串的输入和输出(认识gets()fgets()scanf()和puts()fputs()printf()) ---CCS常用STDIO
- fgets,fputs,gets,puts
- fgets,fputs,gets,puts
- 标准I/O-1-getchar,scanf,gets,fgets,puts,fputs,格式控制
- gets、fgets、puts、fputs、scanf、read、readline、getline等
- gets()、puts()和fgets()、fputs() 的区别。
- gets、puts、fgets、fputs
- fgets,fputs,gets,puts
- fgetc fputc fgets fputs fprintf fscanf getc getchar gets putc putchar puts ungetc函数介绍
- fgets、gets与fputs、puts
- fgets,fputs,gets,puts
- fgets、gets_s和fputs
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
- scanf/gets/puts/fgets
- 字符串读写函数 fgets和fputs 函数