C 在文件中读出指定数据

//在文件中读出指定数据
#include <stdio.h>
#include <stdlib.h>
struct student
{
    
    
	char name[10];
	int num;
	int age;
	char addr[15]; 
}boy;
void main()
{
    
    
	FILE *fp;
	int i=1; //定义结构位置 
	if(!(fp=fopen("student-list","r")))
	{
    
    
		printf("Cannot open the file!\n");
		return;
	} 
	
	rewind(fp);	//定义到文件头 
	
	fseek(fp,i*sizeof(struct student),0);
	fread(&boy,sizeof(struct student),1,fp);
	printf("%s %d %d %s\n",boy.name,boy.num,boy.age,boy.addr);
	system("pause"); 	
} 

猜你喜欢

转载自blog.csdn.net/qq_48167493/article/details/120562438