当前位置:网站首页>笔记:fgets函数详解
笔记:fgets函数详解
2022-07-29 20:49:00 【摁回车的大雷】
对fgets函数的一些测试:
char *fgets(char *s, int size, FILE *stream);
它的功能是从 stream 流中读取 size 个字符存储到字符指针变量 s 所指向的内存空间。它的返回值是一个指针,指向字符串中第一个字符的地址。
其中:s 代表要保存到的内存空间的首地址,可以是字符数组名,也可以是指向字符数组的字符指针变量名。size 代表的是读取字符串的长度。stream 表示从何种流中读取,可以是标准输入流 stdin,也可以是文件流,即从某个文件中读取
一、 每次调用读取的字符个数
file.txt
hello world!
fight for the better future!
test1.c
#include <stdio.h>
#include <stdlib.h>//exit
#include <strings.h>//bzero
int main()
{
FILE* fp;
if(NULL == (fp = fopen("file.txt","r")))
{
printf("打开文件失败!");
exit(-1);
}
char buf[10] = "\0";
int count = 0;
while(NULL != fgets(buf,sizeof(buf),fp))
{
count++;
printf("%s",buf);
bzero(buf,sizeof(buf));
printf("|第%d次循环|",count);
}
return 0;
}
结果分析:

- 假设fgets函数的参数2为 n(n个字节),读取文件流fp时从文件首开始,在不遇到换行时,每调用一次调用读取得到 n-1 个有效字符,fgets 函数自动在读的 n-1个字符后面添加 ‘\0’ ,共保存 n 个字符到参数1中
- 下一次调用 fgets 函数时,从上次读取结束的位置继续读取,如果在读取的前 n-1 个字符时遇到换行,则本次读取到换行符为止,同时仍然在换行符后面添加 ‘\0’ ,一并保存到参数1中
二、 关于覆盖的问题
file.txt
hello world!
fight
for the better future!
test2.c
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
int main()
{
FILE* fp;
if(NULL == (fp = fopen("file.txt","r")))
{
printf("打开文件失败!");
exit(-1);
}
char buf[20] = {
'\0'};
fgets(buf,sizeof(buf),fp);
int i;
for(i=0;i<20;i++)
{
printf("|%c|-",buf[i]);
}
printf("到这里\n\n");
// bzero(buf,sizeof(buf));
fgets(buf,sizeof(buf),fp);
for(i=0;i<20;i++)
{
printf("|%c|-",buf[i]);
}
return 0;
}
结果分析:
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zUon9GZ2-1659097180913)(C:\Users\Pioneer\AppData\Roaming\Typora\typora-user-images\image-20220729200141182.png)]](/img/58/42b06586b838d969f7510c5b5ffad9.png)
- 如果不再第一次输出后清空buf,第二次保存时进来时,buf中仍有上一次读取时保存的字符
- 取消bzero函数的注释后运行结果如下:
![..(img-zUon9GZ2-1659097180913)]](/img/6e/4a77859c736fc1064001c0f89fbcce.png)
边栏推荐
猜你喜欢
![LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode](/img/c2/34624c9c7693ba40d0b3724c0db611.png)
LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode

《张卫国的夏天》欢乐来袭,黄磊、刘奕君携手演绎“冤种”兄弟

南信大提出TIPCB,一个简单但有效的用于基于文本的人员搜索的基于部分的卷积baseline

博世集团启动量子数字孪生计划

高通WLAN框架学习(31)-- Power save

怎么实现您的个人知识库?

剑指 Offer II 097. 子序列的数目

Come in now!!!Take you to know the basic data types of C language

人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准

Second Best PyTorch Beginner Course; Thesis Writing Guide; Using µGo to Develop a Mini Compiler; Super Efficient Use of Transformer's Extension Library; Frontier Papers | ShowMeAI News Daily
随机推荐
Where is Naive Bayes "naive"?
240. Searching 2D Matrix II
C# 窗体与子线程数据交互
错误解决:Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255]
Related phrases include usage and collocation (include)
【593. Valid Square】
无文件落地免杀的初尝试思考(上)
模型推理模板
UDP协议详解
打破原则!MongoDB 引入 SQL?
Unity determines whether a string can be converted to float type
LeetCode 593 有效的正方形[数学] HERODING的LeetCode之路
PyQt5学习一(环境搭建)
The demand for VR live broadcast marketing is increasing, and the data module is paving the way for us
Come in now!!!Take you to know the basic data types of C language
Baidu internship students late night fun: originally giant is this kind of life
微信小程序 31 分包机制
LeetCode 0144. 二叉树的前序遍历:二叉树必会题
Setinel 原理简介
银河麒麟V10 SP2 x86编译安装 PHP7.4