0

私はテキストファイルを持っていて、特定の時間にその特定の部分だけを抽出したかった.そのために、書き込み中に ftell() を使用して開始位置と終了位置を記録し、次に fseek() を使用してその特定の場所にジャンプします.

     int main()
    {
       FILE *fp=fopen("myt","w+");
       char s[80];
       printf ( "\nEnter a few lines of text:\n" ) ;
       while ( strlen ( gets ( s ) ) > 0 )  //user inputs random data
      {                                     //till enter is pressed
       fputs ( s, fp ) ;
       fputs ( "\n", fp ) ;
      }

     long int a=ftell(fp);
     fputs("this line is supposed to be printed only ",fp);//line to be 
                                                           // displayed
     fputs("\n",fp);
     long int b=ftell(fp);
     printf("start is %ld",a);
     printf("\nend is %ld",b);
     printf("here is the data...\n");
     rewind(fp);
     fseek(fp,a,SEEK_CUR);   //move to the starting position of text to be 
                            //displayed
     char x[1000];
     fgets(x,b-a,SEEK_CUR);
     printf("%s",x);
     return 1;
  }

これを試してみましたが、予期せぬプログラムの異常終了に直面しました。タスクを正しく実装する方法を教えてください。

4

1 に答える 1