实例四:随机读写。在C:\\TEST.TXT 文件中创建如下字符串:我爱你,中国  

使用随机读写,输出"中国"两个字,以为中文字符占两个字节,所以要从文件首部向后偏移7个字节,逗号是英文字符,占用一个字节。


#include<stdio.h>

#include <stdlib.h>


int main()

{

  FILE *fp ;

  char c;

  if((fp=fopen("c:\\test.txt","rb"))!=NULL)

  {

      rewind(fp);

      fseek(fp,7L,0);//偏移7个字节

      c=fgetc(fp);

      while(c!=EOF)

      {

        printf("%c",c);

        c=fgetc(fp);

      }

      printf("\n");

  }

  else

  {

     printf("file not exist!");

     exit(1);

  }

  return 0;

}