creat_better
一开尔文小宇
采纳率100%
2021-03-03 14:41

<C语言>在自定义函数中打开main函数中文件

已采纳

#include  <stdio.h>
#define    N    5
typedef struct  student {
  long  sno;
  char  name[10];
  float  score[3];
} STU;
void fun(char  *filename, STU  n)
{ FILE  *fp;
/**********found**********/
  fp = fopen(__1__, "rb+");
/**********found**********/
  fseek(__2__, -(long)sizeof(STU), SEEK_END);
/**********found**********/
  fwrite(&n, sizeof(STU), 1, __3__);
  fclose(fp);
}
main()
{ STU  t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
             {10003,"LiSi", 85, 70, 78},    {10004,"FangFang", 90, 82, 87},
             {10005,"ZhangSan", 95, 80, 88}};
  STU  n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
  int  i,j;      FILE  *fp;
  fp = fopen("student.dat", "wb");
  fwrite(t, sizeof(STU), N, fp);
  fclose(fp);
  fp = fopen("student.dat", "rb");
  fread(ss, sizeof(STU), N, fp);
  fclose(fp);
  printf("\nThe original data :\n\n");
  for (j=0; j<N; j++)
  {  printf("\nNo: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
     for (i=0; i<3; i++)  printf("%6.2f ", ss[j].score[i]);
     printf("\n");
  }
  fun("student.dat", n);
  printf("\nThe data after modifing :\n\n");
  fp = fopen("student.dat", "rb");
  fread(ss, sizeof(STU), N, fp);
  fclose(fp);
  for (j=0; j<N; j++)
  {  printf("\nNo: %ld  Name: %-8s      Scores:  ",ss[j].sno, ss[j].name);
     for (i=0; i<3; i++)  printf("%6.2f ", ss[j].score[i]);
     printf("\n");
  }
  getchar();
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • qq_40344524 书山客 1月前

    1、filename

    2、fp

    3、fp

    点赞 1 评论 复制链接分享
  • creat_better 一开尔文小宇 1月前

    fopen("文件地址","操作");

    点赞 评论 复制链接分享
  • creat_better 一开尔文小宇 1月前

    filename 文件地址,fopen("1地址""2")

    点赞 评论 复制链接分享

为你推荐