将键盘输入的几个数据存储到文件里的程序

#include<stdio.h>
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];        //定义结构体数组完成数据的存储

void save()
{
FILE *fp;
int i;
if((fp=fopen("stu_list.txt","wb"))==NULL)   //创建文件名称,使数组中保存数据
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student_type),1,fp)!=1)      将运用fwrite函数进行数据的转存
printf("file write error\n");
fclose(fp);
}

void main()
{
int i;
for(i=0;i<SIZE;i++)
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);        //输入学生的数据—保存在数组里面
save();      调用函数对文件进行操作
}

原文地址:https://www.cnblogs.com/hao02171990/p/3008656.html