C语言中,如何将一个结构体内的信息全部输入到一个文件中
2个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
char name[20];
int age;
char num[20];
}stu;
void write()
{
FILE *file;
if((file=fopen("C:\\123","wb"))==NULL)
{
printf("open file failed!\n");
exit(0);
}
//stu stu1;
strcpy(stu.name,"张三");
strcpy(stu.num,"09111626");
stu.age=19;
fwrite(&stu,sizeof(struct student),1,file);
fclose(file);
}
void read()
{
FILE *file;
if((file=fopen("C:\\123","rb"))==NULL)
{
printf("open file failed!\n");
exit(0);
}
//stu stu2;
fread(&stu,sizeof(struct student),1,file);
fclose(file);
printf("name=%s,age=%d,num=%s\n",stu.name,stu.age,stu.num);
}
void main()
{
//write();
read();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询