C语言结构体 数组输入 查询 急
发布网友
发布时间:2022-05-24 17:10
我来回答
共2个回答
热心网友
时间:2023-10-22 17:53
#include <stdio.h>
#include <string.h>
struct teacher
{
char name[20];
int age;
char course[20];
};
int main()
{
FILE *fptr;
teacher one;
char name[20];
if((fptr = fopen("teachers.txt", "r+")) == NULL)
{
return 0;
}
//insert teacher infor into the file
fseek(fptr, 0, SEEK_END);
scanf("%s%d%s", one.name, &one.age, &one.course);
fprintf("%s%d%s", one.name, one.age, one.course);
//query teacher infor by name
fseek(fptr, 0, SEEK_SET);
scanf("%s", name);
do
{
fscanf("%s%d%s", one.name, &one.age, one.course);
if(strcmp(one.name, name) == 0)
{
printf("%20s%10d%20s\n", one.name, one.age, one.course);
break;
}
}
while(fptr != eof);
return 0;
}
热心网友
时间:2023-10-22 17:53
自己定义一个结构体,再运用文件处理的做法来对文件信息进行输入输出查询工作即可!