fscanf rewind

/* 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> int findHead(FILE * ,int); int findLast(FILE * ,int); void deleteState(FILE **,int * ,int *); void deleteLine(FILE **,int ,int ); int main(){ FILE * fp = fopen("dict.dic","r"); int start,end; if(fp != NULL){ int a,b,c,d,e; fscanf(fp,"%d",&a); fscanf(fp,"%d",&b); fscanf(fp,"%d",&c); fscanf(fp,"%d",&d); fscanf(fp,"%d",&e); int mode; scanf("%d",&mode); switch (mode) { case 1: rewind(fp); start = findHead(fp,5); rewind(fp); end = findLast(fp,a); break; case 2: rewind(fp); start = findHead(fp,a); rewind(fp); end = findLast(fp,b); break; case 3: rewind(fp); start = findHead(fp,b); rewind(fp); end = findLast(fp,c); break; case 4: rewind(fp); start = findHead(fp,c); rewind(fp); end = findLast(fp,d); break; case 5: rewind(fp); start = findHead(fp,d); rewind(fp); end = findLast(fp,e); break; default: printf("??"); break; } rewind(fp); while(ftell(fp)<start) fgetc(fp); while(ftell(fp) != end){ int currentch = fgetc(fp); int flag = 1; if(currentch == '/' ){ int nextch = fgetc(fp); if(nextch == ''){ while(flag && ftell(fp) != end){ currentch = fgetc(fp); while(currentch == '' && ftell(fp) != end){ currentch = fgetc(fp); if(currentch == '/' && ftell(fp) != end) flag = 0; } } } else if(nextch == '/'){ while(currentch != '\n'){ currentch = fgetc(fp); } } else { printf("%c",currentch); currentch = nextch; } } if(ftell(fp) != end && flag) printf("%c",currentch); } } fclose(fp); return 0; } int findHead(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); } int findLast(FILE * fp,int line){ int ch = 0; while(line-- && ch != EOF){ ch = fgetc(fp); while(ch != '\n' && ch != EOF){ ch = fgetc(fp); } } return ftell(fp); }将子函数并到主函数里
05-12

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值