fseek 用法

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#pragma warning(disable : 4996)


struct Student
{
char name[20];
int num;
int age;
float scor;
};
int main()
{
//fscanf,fprintf,从硬盘上读取文件,和scanf printf差不多,后者是从显示器上读取文件

//fseek 用法
struct Student boys[3];
struct Student boy;
struct Student *pBoys;
FILE *fp;

pBoys = boys;
fp = fopen("test.txt", "wb+");
if (fp == NULL)
{
puts("不能打开该文件\n");
getch();
exit(0);
}
printf("请输入学生的相关数据\n");
for (size_t i = 0; i < 3; i++)
{
scanf("%s %d %d %f",&pBoys->name,&pBoys->num,&pBoys->age,&pBoys->scor);
pBoys++;
}
fwrite(boys, sizeof(struct Student), 3, fp);
fseek(fp, sizeof(struct Student), SEEK_SET);

fread(&boy, sizeof(struct Student), 1, fp);//读取第二个学生的信息
printf("%s %d %d %f", boy.name, boy.age, boy.age, boy.scor);
fclose(fp);

 

 

 

 


return 0;
}

posted @ 2021-09-08 10:03  江南王小帅  阅读(321)  评论(0)    收藏  举报