【C语法学习】3 - fgetc()函数

1 函数原型

fgetc():从指定流stream中读取一个字符,函数原型如下:

int fgetc(FILE *stream)

cstdio库描述如下:

1. Get character from stream.
2. Returns the character currently pointed by the internal file position indicator of the specified stream. The internal file position indicator is then advanced to the next character.
3. If the stream is at the end-of-file when called, the function returns EOF and sets the end-of-file indicator for the stream (feof).
4. If a read error occurs, the function returns EOF and sets the error indicator for the stream (ferror).

2 参数

fgetc()函数只有一个参数stream:

  1. 参数stream是一个指向FILE类型结构的指针;stream指定了fgetc()函数要读取的流,可以是文件流,也可以是标准输入流;当是文件流时,stream等于fopen()函数的返回值;当是标准输入流时,stream等于stdin。

cstdio库描述如下:

1. Pointer to a FILE object that identifies an input stream.

3 返回值

fgetc()函数的返回值类型为int型:

  1. 读取成功,以无符号char强制转换为int的形式(整型提升)返回读取的字符;
  2. 读取失败,返回EOF。

关于读取失败,分为两种情况:

  1. 读取遇到文件末尾(疑问:从stdin中读取字符怎么会遇到文件末尾);
  2. 读取过程发生错误(疑问:从stdin中读取字符会发生什么样的错误)。

cstdio库描述如下:

1. On success, the character read is returned (promoted to an int value).
2. The return type is int to accommodate for the special value EOF, which indicates faliure.
3. If the standand input was at the end-of-file, the function returns EOF and sets the eof indicator (feof) of stdin.  
4. If some other reading error happens, the function also returns EOF, but sets its error indicator (ferror) instead.

4 比较

fgetc()函数和getchar()函数的工作原理类似,差异如下:

  1. fgetc()函数从指定流stream中读取字符;
  2. getchar()函数从标准输入流stdin中读取字符;
  3. 将fgetc()函数的参数stream指定为stdin,则fgetc()函数的功能和getchar()函数的功能完全相同。

5 示例

5.1 示例1

使用fgetc()函数从文件读取字符并打印,代码如下所示:

int main()
{
   FILE* fp;
   char  ch;

   if ((fp = fopen("1.txt", "r")) == NULL)
   {
      printf("Failed to open file.\n");
      exit(1);
   }

   while ((ch = fgetc(fp)) != EOF)
   {
      putchar(ch);
   }

   printf("\n");

   fclose(fp);

   return 0;
}

文件包含内容如下图所示:

在这里插入图片描述

代码运行结果如下图所示:

在这里插入图片描述

5.2 示例2

使用fgetc()函数从标准输入流stdin读取字符串"hello world"并打印,代码如下所示:

int main()
{
   //
   char ch = 0;
   while ((ch = fgetc(stdin)) != '\n' && ch != EOF)
   {
      putchar(ch);
   }
   //
   printf("\n");
   //
   return 0;
}

代码运行结果如下图所示:

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`fseek()`函数是用于在标准C库中进行文件定位的函数。它的语法如下: ```c int fseek(FILE *stream, long offset, int origin); ``` 参数解释: - `stream`:指向文件的指针,即文件流。 - `offset`:偏移量,表示要移动的字节数。正值表示向文件末尾方向移动,负值表示向文件开头方向移动。 - `origin`:起始位置,可以是以下值之一: - `SEEK_SET`:从文件开头开始计算偏移量。 - `SEEK_CUR`:从当前位置开始计算偏移量。 - `SEEK_END`:从文件末尾开始计算偏移量。 返回值: - 如果成功,返回0。 - 如果发生错误,返回非零值。 下面是一个使用`fseek()`函数的例子: ```c #include <stdio.h> int main() { FILE *fp = fopen("example.txt", "r"); if (fp == NULL) { perror("Failed to open file"); return 1; } // 将文件指针定位到文件开头 fseek(fp, 0, SEEK_SET); // 读取文件中的字符 int ch; while ((ch = fgetc(fp)) != EOF) { putchar(ch); } fclose(fp); return 0; } ``` 在上面的例子中,我们首先打开一个名为"example.txt"的文件。然后,使用`fseek()`函数将文件指针定位到文件开头。接下来,我们使用`fgetc()`函数逐个字符地读取文件内容,直到文件结束。最后,关闭文件。 这个例子展示了如何使用`fseek()`函数进行文件定位,并结合`fgetc()`函数读取文件内容。你可以根据需要修改偏移量和起始位置,以便在文件中进行定位操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值