fscanf rewind

本文通过一个示例展示了如何使用fscanf从文件中读取浮点数和字符串。首先创建并写入数据到myfile.txt,然后使用rewind重置文件位置指针到开始处,接着读取之前写入的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* fscanf example */
#include <stdio.h>
 
int main ()
{
  char str [80];
  float f;
  FILE * pFile;
 
  pFile = fopen ("myfile.txt","w+");
  fprintf (pFile, "%f %s", 3.1416, "PI");
  rewind (pFile);
  fscanf (pFile, "%f", &f);
  fscanf (pFile, "%s", str);
  fclose (pFile);
  printf ("I have read: %f and %s \n",f,str);
  return 0;
}
  
 
This sample code creates a file called myfile.txt and writes a float number and a string to it. Then, the stream is rewinded and both values are read with fscanf. It finally produces an output similar to:
 
I have read: 3.141600 and PI




void rewind ( FILE * stream );
Set position of stream to the beginning

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值