要求:
1、定义结构体Student
2、定义结构体变量
3、给结构体变量赋值
4、利用fputs把结构体变量的内容写入到student.dat
5、利用fgets把student.dat的内容读取出来
struct Student
{
char Id[20];
char Name[20];
char Sex;
int Age;
}Student[100];
int num = 0;
char pathname[] = "C:/Users/zhang.lei/Desktop/1.txt";
void output()
{
char buff[128] = { 0 };
FILE* fp = fopen(pathname,"w");
if (fp == NULL) printf("pathname error \n");
for (int i = 0 ; i < num ; i++)
{
sprintf(buff,"%s %s %c %d \n", Student[i].Id, Student[i].Name, Student[i].Sex, Student[i].Age);
fputs(buff,fp);
}
fclose(fp);
}
void input()
{
char buff[128] = { 0 };
FILE* fp = fopen(pathname, "r");
if (fp == NULL) printf("pathname error \n");
num = 0;
while(fgets(buff,128,fp) != NULL)
{
sscanf(buff, "%s %s %c %d \n", Student[num].Id, Student[num].Name, &Student[num].Sex, &Student[num].Age);
//puts(buff);
num++;
}
fclose(fp);
}
给你写一个吧
在CSDN里搜索数据结构IO学生信息管理,会有很多的实例,参考一下,你这个要求很简单的。
#include<stdio.h>
struct student
{
int num;
char name[20];
char sex;
int age;
};
int main()
{
int n;
scanf("%d",&n);
student information[20];
student* p=information;
for(int i=0;i<n;i++)
{
scanf("%d %s %c %d",&(p+i)->num,(p+i)->name,&(p+i)->sex,&(p+i)->age);
}
for(int i=0;i<n;i++)
printf("%d %s %c %d\n",(p+i)->num,(p+i)->name,(p+i)->sex,(p+i)->age);
return 0;
}