2012-10-08 56 views
1

我有这一点功课:搜索文本文件的ID(Turbo C的)

给定一个文本文件,创建一个搜索用户提供特定的ID ,里面的文件功能。如果它存在,打印整个 简介如下:

ID_genre_name_age_height

在文本文件中,只有一个轮廓:

19800372_male_David_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(); 
} 

我想不通为什么它不会工作,我输入ID,并在那里停留。

+0

@Jason - Please note:[The homework tag is obsolete](http://meta.stackexchange.com/q/147100/179533)。我回滚了。 – ArjunShankar

+1

函数'gets()'不能安全使用。请从您的程序中删除它。它也不再是C语言的一部分(自2011年12月发布最新标准以来)。 – pmg

+0

偏离主题,仍然:考虑使用不同于Turbo-C的编译器。它非常非常老,可能根本不用于任何严肃的项目。考虑避免使用非标准函数,如'clrscr()'和'getch()'。 – ArjunShankar

回答

2
  1. 检查fopen是否成功。

  2. 如果您的输入文件有:ID_genre_name_age_height那么这将被视为单个字符串。由于您似乎将它们读入变量,因此您应该将输入文件作为空格分隔字符串的列表。

  3. 所有的变量都是char数组,但是对于某些未定义的行为,您使用%f。适当地改变你的变量类型。

  4. 检查fscanf()的返回值以查看没有读取错误。

  5. feof告诉你是否读取过去的输入文件,而不是文件的结尾。这意味着该循环将被执行超过您所期望的一次。考虑类似fgets然后做一个sscanf()

0

您的fscanf转换和变量类型不匹配。

 fscanf(in, "%.0f %s %s %d %.2f", ced, sex, nombre, edad, altura); 

cedpointer to float;它是char[10];使用"%9s"代替

sex和​​几乎确定:有

edad应该pointer to int缓冲区溢出的危险:它是不确定的:它定义并使用正确的转换

altura应该pointer to float:它是char[5]:请使用"%4s"而不是

0

重读文档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] 
+0

他使用的是Turbo C,在强制C标准时可能会有更多的自由度(取决于他使用的Turbo C的版本,C标准可能还没有在那个时候发布)。当然不会被弃用(尽管使用它仍然是一个坏主意,但显然人们对此知之甚少)。 – Cubic

+0

感谢您的快速回复。我来自委内瑞拉,这就是为什么部分代码是西班牙文,我忘了翻译它,我使用turbo c,bcuz这是我的老师“使用”,他不会接受在另一个编译器中编写的程序,例如作为dev C++,我更喜欢。我目前正在对代码进行一些更改(如此处所述),我将编辑主代码,以便您可以检查它。 – user1730002

+0

user1730002您仍然可以使用KingsIndian对我的大多数警告的回复来更好地了解您的代码出了什么问题。 – mmlb