完全详解fgets()函数!

关于fgets()函数的描述如下:

fgets()

Read a string of characters from a stream

Synopsis:

#include <stdio.h>

char* fgets( char* buf, 
             size_t n, 
             FILE* fp );

Arguments:

buf
A pointer to a buffer inwhich   fgets()  canstore the characters that it reads.
n
The maximum number of characters to read.
fp
The stream from which to read the characters.

Description:

The fgets() functionreads a string of characters from the stream specifiedby fp, and stores them in thearray specified by buf.

It stops reading characters when:

  • the end-of-file is reached

    Or:

  • a newline ('\n') character is read

    Or:

  • n-1 characters have been read.

The newline character isn't discarded. A null character is placedimmediately after the last character read into the array.



我们利用以下代码来测试:


#include<stdio.h>


#define BUF_SIZE 10


int main()
{
    FILE *fp;
    char *buf[BUF_SIZE];
   if((fp=fopen("data","r"))==NULL)
    {
      printf("can't open the file!\n");
      exit(1);
    }
   fgets(buf,BUF_SIZE,fp);
    printf("thebuf is : %s!",buf);
    return0;
}



外加一个测试文件叫‘data’,里面内容如下:
0123456789
0123456789

结果如下:
BUF_SIZE=10

BUF_SIZE=11

BUF_SIZE=12


代码的作用是从'data'里面读取第一行内容,我们的主要目的是看换行符是怎么被处理的。
通过上面的调试查看内存,我们可以很清楚的看到fgets()函数的行为:
缓冲区的最后一个空间总是用来存储'\0'的,也就是说实际缓冲区最多只能放n-1个字符,而换行符\n也是被看作普通字符来处理的

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux中,fgets函数是用来从指定文件流中读取一行数据的。它的用法如下所示: ```c char *fgets(char *str, int n, FILE *stream); ``` 其中,str是用来存储读取数据的字符数组,n是指定最大读取字符数(包括换行符和空字符),stream是指定的文件流。 举个例子,下面的代码演示了如何使用fgets函数从文件中读取一行数据,并打印出来: ```c #include <stdio.h> int main() { FILE *fp; char readBuff = {0}; fp = fopen("file.txt", "r"); // 打开文件(以只读模式) if (fp == NULL) { printf("open file failed\n"); return -1; } fgets(readBuff, 128, fp); // 读取文件中的一行数据到readBuff数组中 printf("readBuff is %s\n", readBuff); // 打印读取到的数据 fclose(fp); // 关闭文件 return 0; } ``` 这段代码会打开名为file.txt的文件(假设文件存在),然后使用fgets函数从文件中读取一行数据到readBuff数组中,最后将读取到的数据打印出来。 请注意,fgets函数会在读取到指定的最大字符数n前停止读取,或者在读取到换行符或文件结束符时停止读取。因此,如果一行数据的长度超过了指定的最大字符数,fgets函数会将剩余的字符留在输入流中,直到下一次读取。 引用:<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【Linux篇】fputs、fgets函数](https://blog.csdn.net/m0_66492811/article/details/129105448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [linux文件的操作函数用法详解](https://download.csdn.net/download/weixin_39247141/10611546)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值