70,037
社区成员
发帖
与我相关
我的任务
分享#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct student_score
{
char name[10];
int num;
int score[3];
} stu[8];
void main()
{
int i;
FILE *fp, *fp1;
if((fp=fopen("stu.txt","r"))==NULL)
{
printf("Can't open file\n");
exit(0);
}
fp1=fopen("result.txt","w");
for(i=1;i<8; i+=2)
{
fseek(fp,sizeof(struct student_score)+1,1); /*fp从当前位置向前移动一个student_score结构大小字节,“+1”是每条记录多一个换行符*/
fread(&stu[i],sizeof(struct student_score),1,fp);
fwrite(&stu[i],sizeof(struct student_score),1,fp1);
printf("%s\n",stu[i].name);
}
fclose(fp);
fclose(fp1);
} #include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct student_score
{
char name[10];
int num;
int score[3];
} stu[8];
int main()
{
int i;
FILE *fp, *fp1;
if((fp=fopen("stu.txt","r"))==NULL)
{
printf("Can't open file\n");
exit(0);
}
fp1=fopen("result.txt","w");
for(i=1;i<8; i+=2)
{
char buf[128]={0};
fgets(buf,128,fp);
fgets(buf,128,fp);
fputs(buf,fp1);
printf("%s\n",stu[i].name);
}
fclose(fp);
fclose(fp1);
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct student_score
{
char name[10];
int num;
int score[3];
} stu[8];
void main()
{
int i, j;
FILE *fp, *fp1;
if((fp=fopen("stu.txt","r"))==NULL)
{
printf("Can't open file\n");
exit(0);
}
fp1=fopen("result.txt","w");
for(i=1;i<8; i+=2)
{
fscanf(fp, "%*[^\n]%*c");
fscanf(fp, "%d", &stu[i].num);
fprintf(fp1, "%d ", stu[i].num);
fscanf(fp, "%s", stu[i].name);
fprintf(fp1, "%s ", stu[i].name);
for(j = 0; j < 3; j++)
{
fscanf(fp, "%d", &stu[i].score[j]);
fprintf(fp1, "%d ", stu[i].score[j]);
}
fscanf(fp, "%*[^\n]%*c");
fprintf(fp1, "\n");
}
fclose(fp);
fclose(fp1);
}