C fgets()函数用法示例读取文件

Standard C library provides the fgets() function to read a line from a specified stream where the stream can be a file. fgets() function also used to read the specified number or size of the characters from the given string. In this tutorial, we will learn the usage of fgets()  function with its parameters with different examples.

标准C库提供了fgets()函数,以从指定的流读取一行,该流可以是文件。 fgets()函数还用于从给定的字符串中读取指定数量或大小的字符。 在本教程中,我们将通过不同的示例学习fgets()函数及其参数的用法。

fgets()函数语法 (fgets() Function Syntax)

The syntax of the fgets()  function is very easy. there are only 3 parameters where we will get a char array as a return value.

fgets()函数的语法非常简单。 只有3个参数,我们将获得一个char数组作为返回值。

char *fgets(char *str, int n, FILE *stream)

Here is the parameters meaning and usage information.

这是参数的含义和用法信息。

  • char *str is a string value where copied or got string will be stored.

    char *str是一个字符串值,将在其中存储复制或获取的字符串。

  • int n is the size or count of the read characters.

    int n是读取字符的大小或计数。

  • FILE *stream is the stream we want to read which is generally a file.

    FILE *stream是我们要读取的流,通常是一个文件。

fgets()函数的返回值 (Return Value of fgets() Function)

If the function execution is completed successfully there is a return value which is a char. If the end of stream or file is reached and no characters have been read the contents of str remain unchanged and a null pointer is returned.

如果函数执行成功完成,则返回值为char的返回值。 如果到达流或文件的末尾且未读取任何字符,则str的内容保持不变,并返回空指针。

从文件中读取整行 (Read The Whole Line From A File)

Now its time for example. We will create a sample code which will read the file file.txt .  The string read will be put to the string.

现在是时候了。 我们将创建一个示例代码,该示例代码将读取文件file.txt 。 读取的字符串将放入字符串中。

  • char str[] is the char array we will store the string we read.

    char str[]是char数组,我们将存储读取的字符串。

  • FILE *f is the file pointer we will use to read the string.

    FILE *f是我们将用来读取字符串的文件指针。

#include <stdio.h>

int main () {
   //File pointer to store opened file information and cursor
   FILE *f;

   //str char array to stored read values or characters
   char str[160];

   /* opening file for reading */
   f = fopen("file.txt" , "r");
   if(f == NULL) {
      perror("Error opening file");
      return(-1);
   }
   if( fgets (str, 160, f)!=NULL ) {
      /* writing content to stdout */
      puts(str);
   }
   fclose(f);
   
   return(0);
}
Read From File Whole Line
Read From File Whole Line
从文件整行读取

We will store the code above with the file name fgets.c and we will compile with the following command.

我们将使用文件名fgets.c存储上面的代码,并使用以下命令进行编译。

$ gcc fgets.c -o fgets

AND  run the created fgets executable like below.

然后运行创建的fgets可执行文件,如下所示。

$ ./fgets
Read From File Whole Line
Read From File Whole Line
从文件整行读取

从文件中读取指定字节数或大小的行(Read Line For Specified Number Of Bytes or Size From A File)

In this example, we will specify the string size we want to read. The size value will be provided as the second parameter of the fgets()  function. In this example, we will read 300 characters from the given file to the str char array.

在此示例中,我们将指定我们要读取的字符串大小。 大小值将作为fgets()函数的第二个参数提供。 在此示例中,我们将从给定文件中读取300个字符到str char数组。

#include <stdio.h>

int main () {
   //File pointer to store opened file information and cursor
   FILE *f;

   //str char array to stored read values or characters
   char str[300];

   /* opening file for reading */
   f = fopen("file.txt" , "r");
   if(f == NULL) {
      perror("Error opening file");
      return(-1);
   }
   if( fgets (str, 300, f)!=NULL ) {
      /* writing content to stdout */
      puts(str);
   }
   fclose(f);

   return(0);
}
LEARN MORE  What Is EOF (End Of File)? Examples with PHP, C++, C, Python, Java
了解更多什么是EOF(文件末尾)? PHP,C ++,C,Python,Java的示例

翻译自: https://www.poftut.com/fgets-c-library-function-usage-examples-to-read-file/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值