在文本文件中搜索ID(Turbo c)
我有一个小作业: 给定一个文本文件,创建一个搜索特定ID的函数 由用户提供,在文件中。如果存在,请打印整个文档 简介如下:在文本文件中搜索ID(Turbo c),c,turbo-c,C,Turbo C,我有一个小作业: 给定一个文本文件,创建一个搜索特定ID的函数 由用户提供,在文件中。如果存在,请打印整个文档 简介如下: #include <conio.h> #include <fcntl.h> #include <string.h> #include <stdio.h> #include <iostream.h> int menu (); main () { int dato; void create(); void Stor
#include <conio.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
int menu ();
main ()
{
int dato;
void create();
void Store();
void read();
void search();
clrscr();
do
{
dato=menu();
switch (dato)
{
case 1:
create();
break;
case 2:
store();
break;
case 3:
read();
break;
case 4:
search();
break;
case 5:
return -1;
default: cout<<"\n Error";
getch ();
break;
}
}
while (dato !=5);
getch();
return 0;
}
int menu ()
{
int op;
clrscr();
cout<<"\n File creator system";
cout<<"\n1 Create a file";
cout<<"\n2 Store Information";
cout<<"\n3 Read a file";
cout<<"\n4 Search Information";
cout<<"\n5 Exit...\n";
cout<<"\n";
cin>>op;
return op;
}
.
.
.
.
void search()
{
char ID[10],
char ID1[10];
char genre[10];
char name[20];
int age;
float height;
FILE *in;
in=fopen("c:\\exercise.txt","r");
clrscr();
printf("Enter ID: ");
fgets (ID1,10,stdin);
do{
fscanf(in,"%9s %s %s %d %4s",ID,genre,name,age,height);
if (strcmpi(ID,ID1)==0)
{
printf("ID:%9s\n",ID);
printf("Genre:%s\n",genre);
printf("Name:%s\n",name);
printf("Age:%d\n",age);
printf("Height:%4s\n",height);
}
}while(!feof(in));
fclose(in);
getch();
}
ID\u流派\u姓名\u年龄\u身高
文本文件中只有一个配置文件:
19800372_男性_大卫_19_1.75
因此,如果我输入相同的ID,我的函数应该打印这些信息
我的代码目前不起作用,如下所示:
#include <conio.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
int menu ();
main ()
{
int dato;
void create();
void Store();
void read();
void search();
clrscr();
do
{
dato=menu();
switch (dato)
{
case 1:
create();
break;
case 2:
store();
break;
case 3:
read();
break;
case 4:
search();
break;
case 5:
return -1;
default: cout<<"\n Error";
getch ();
break;
}
}
while (dato !=5);
getch();
return 0;
}
int menu ()
{
int op;
clrscr();
cout<<"\n File creator system";
cout<<"\n1 Create a file";
cout<<"\n2 Store Information";
cout<<"\n3 Read a file";
cout<<"\n4 Search Information";
cout<<"\n5 Exit...\n";
cout<<"\n";
cin>>op;
return op;
}
.
.
.
.
void search()
{
char ID[10],
char ID1[10];
char genre[10];
char name[20];
int age;
float height;
FILE *in;
in=fopen("c:\\exercise.txt","r");
clrscr();
printf("Enter ID: ");
fgets (ID1,10,stdin);
do{
fscanf(in,"%9s %s %s %d %4s",ID,genre,name,age,height);
if (strcmpi(ID,ID1)==0)
{
printf("ID:%9s\n",ID);
printf("Genre:%s\n",genre);
printf("Name:%s\n",name);
printf("Age:%d\n",age);
printf("Height:%4s\n",height);
}
}while(!feof(in));
fclose(in);
getch();
}
#包括
#包括
#包括
#包括
#包括
int菜单();
主要()
{
国际达托;
void create();
无效存储();
无效读取();
无效搜索();
clrsc();
做
{
dato=菜单();
开关(dato)
{
案例1:
创建();
打破
案例2:
store();
打破
案例3:
read();
打破
案例4:
搜索();
打破
案例5:
返回-1;
默认值:cout
检查fopen
是否成功
如果您的输入文件有:ID\u genre\u name\u age\u height
,则这将被视为一个字符串。因为您似乎将它们读入变量,所以您的输入文件应该是一个以空格分隔的字符串列表
所有变量都是字符数组,但对某些未定义的行为使用%f
。请适当更改变量类型
检查fscanf()的返回值以查看是否存在读取错误
<> >代码> > FoF< /Cord>告诉你是否读过输入文件,而不是文件结尾。这意味着循环将再一次执行,比你所希望的要多。考虑一些类似于<代码> FGES >然后做一个<代码> SCASNFER()/<代码> < /P>
fscanf转换和变量类型不匹配
fscanf(in, "%.0f %s %s %d %.2f", ced, sex, nombre, edad, altura);
ced
应该是指向浮点的指针;它是char[10]
;使用%9s
sex
和nombre
几乎没有问题:存在缓冲区溢出的危险
edad
应该是指向int的指针:它未定义:定义它并使用正确的转换
altura
应该是指向float的指针
:它是char[5]
:使用%4s”
,而不是重新阅读fscanf
的文档,如果您使用不正确,请检查返回代码!编译器也应该给您一个警告(或9),如果您没有警告,那么您会感到羞耻
t.c: In function ‘search’:
t.c:13:5: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
t.c:16:9: warning: unknown conversion type character ‘.’ in format [-Wformat]
t.c:16:9: warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘char *’ [-Wformat]
t.c:16:9: warning: unknown conversion type character ‘.’ in format [-Wformat]
t.c:16:9: warning: too many arguments for format [-Wformat-extra-args]
t.c:19:13: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘char *’ [-Wformat]
t.c:22:13: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]
t.c:23:13: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘char *’ [-Wformat]
t.c:6:46: warning: unused variable ‘age’ [-Wunused-variable]
@Jason-请注意:。我正在回滚。函数get()
无法安全使用。请将其从程序中删除。此外,它不再是C语言的一部分(自2011年12月最新标准发布以来)考虑到主题不同,还是考虑:使用与Turbo C不同的编译器。它非常古老,可能根本不用于任何严肃的项目。考虑避免非标准函数如<代码> CLSRR()<代码>和<代码> GETCH()。
也是。@Arjun这很可能是他们在学院/学校使用的工具集。出于某种原因,一些所谓的“教育者”非常喜欢过时的(通常是有缺陷的)工具。@立方体-这将是不幸的。UsRe173000。如果这是真的,你甚至应该考虑与你的老师谈论使用更现代的东西。GCC是很棒的,免费的,免费的,例如,无论如何,祝他好运。他使用Turbo C,这可能是更自由的实施C标准。(取决于他使用的Turbo C的版本,C标准当时可能还没有发布)。gets在当时当然没有遭到反对(尽管使用它仍然是一个坏主意,但显然很少有人知道这一点)谢谢你的快速回复。我来自委内瑞拉,这就是为什么部分代码是西班牙语,我忘了翻译,我用的是turbo c,bcuz,这就是我的老师“使用的”而且他不接受其他编译器中的程序,比如DEV C++。我现在对代码做了一些修改,如这里所说的,我将编辑主代码,这样你们就可以检查它。USER 173000 02,您仍然可以使用金信甸的响应,用我的大多数警告来更好地了解您的代码有什么问题。