//编写一个程序,可以查询用户信息--通过姓名
用fscanf读取文件信息
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//编写一个程序,可以查询用户信息--通过姓名
int main()
{
FILE* file;
char input_name[20];
char name[20];
char tel[20];
char add[20];
int ret = 0;
file = fopen("demo.txt", "r");
if (!file) {
puts("open fail");
return -1;
}
p1:
//1、输入名字
puts("请输入查询人的姓名:");
scanf("%s", input_name);
while (getchar() != '\n');
while (!feof(file)) {//循环判断
//2、获取文件中的数据
fscanf(file, "%s tel:%s add:%s", name, tel, add);
if (!strcmp(name, input_name))
{
ret = 1;
break;
}
}
if (ret) {
printf("%s tel:%s add:%s\n", input_name, tel, add);
}
else {
printf("没有%s的信息\n", input_name);
puts("输入y:继续,其他退出");
char select;
select = getchar();
if (select == 'y') {
fseek(file, 0, SEEK_SET);
goto p1;
}
}
fclose(file);
return 0;
}
文件内容
nike tel:15101223262 add:wuhan
adidas tel:15555555562 add:北京
jack tel:15445732762 add:上海
aka47 tel:15213213262 add:汕头
执行结果:
有乱码不是问题是中文乱码,格式问题,调一下就好了,或者使用英文
请输入查询人的姓名:
hello
没有hello的信息
输入y:继续,其他退出
y
请输入查询人的姓名:
aka47
aka47 tel:15213213262 add:姹曞ご
E:\C\visualStudio\demo\find_tel\x64\Debug\find_tel.exe (进程 11472)已退出,代码为 0。
按任意键关闭此窗口. . .
4536

被折叠的 条评论
为什么被折叠?



