well I've this little homework.

"Given a text file, create a function that search an specific ID provided by the user inside the file, If exits, print the entire profile as follow:

ID_genre_name_age_height"

So In the text file there is only one profile.

19800372_male_David_19_1.75

so, my function should print that information If I introduce the same ID.

My code which don't work so far Is this one.

void search()

{

char ced1[10],ced[10],nombre[20],sex[10],age[10],altura[5];


FILE *in;
in=fopen("c:\\ex.txt","r");

clrscr();
printf("Enter ID: ");
gets (ced1);

do{

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

  {
printf("Cedula:%.0f\n",ced);
printf("Sexo:%s\n",sex);
printf("Nombre:%s\n",nombre);
printf("Edad:%d\n",edad);
printf("altura:%.2f\n",altura);
}

}
while(!feof(in));
fclose(in);


getch();
} 

Im sorry, I forget to rename the code, I'm from venezuela, so I wrote It in spanish. this is the translated code.

This is part of my code, since my program has such things as, create a file, store data in such file, read all the info In that file, and so on... thats why I didn't put the headers I was using, anyway this is pretty much how the main looks like. Hope you guys can help me out.

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

Edited 4 Years Ago by DavdPrez

Im sorry, I forget to rename the code, I'm from venezuela, so I wrote It in spanish. this is the translated code.

That was unnecessary. I understood enough to see what you were doing wrong -- hence my response.

This is part of my code, since my program has such things as, create a file, store data in such file, read all the info In that file, and so on... thats why I didn't put the headers I was using, anyway this is pretty much how the main looks like.

I see you understood my previous post. That's good.

Hope you guys can help me out.

With what? You didn't mention there were any problems.

The only thing I see is you are using conio.h. Don't. It's not portable and once you learn to rely on it, when you change compilers you'll be crippled.

Also, main() is an INT. Most good compilers won't compile a program that don't use a correct definition.

The function "search" should print the Info that come along with the ID, once It Is found.

but It doesn't, I mean, In the file there this profile:

19800372_male_David_19_1.75

So, If I put 19800372, the program should look If that ID Is In the file, If so, retrieve the profile with it, the above one.

but once I input the ID, nothing happens. thats my problem, Thanks.

Ps: I'm using Turbo C, because thats what my teacher uses, and he won't accept any programs made In another compiler, such as Dev c++ for example, which I prefer for that sake.

Edited 4 Years Ago by DavdPrez

Are you sure what you typed in (not put, by the way) is correctly in the variable ID1?
Are you sure what you read in from the file is correctly in the variable ID?

Print them out and see. Or use the debugger.

Ps: I'm using Turbo C, because thats what my teacher uses, and he won't accept any programs made In another compiler, such as Dev c++ for example, which I prefer for that sake.

You don't have to keep apologizing for things. You didn't need to rewrite your program into English, and no one said anything about not using Turbo-C.

Hi, thanks again for the quick replies.

I mentioned about the turbo c, and rewrite, just for the sake of clarity. I felt It was quite messy a code with spanglish In it.

Are you sure what you typed in (not put, by the way) is correctly in the variable ID1?.
Are you sure what you read in from the file is correctly in the variable ID?.

I did a few changes to the code.

FILE *in;
 in=fopen("c:\\exercise.txt","r");
  clrscr();
  printf("Enter ID: ");
  cin>>ID1;
  cout<<ID1;
 do{
    fscanf(in,"%9s %s %s %d %4s",ID,genre,name,age,height);
    cout<<ID;
  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();
 } 

So, In fact ID1 gets the number I typed in "20912693".

I added a test line "cout<<ID;" after the fscanf.
to my surprise It displays some of the information but not as expected:

I was expecting: 20912693_male_david_19_1.75
This is what I get : david_19_1.75

At this point, Im completely clueless whats going on there. I'm trying to workout the
"if (strcmpi(ID,ID1)==0)" line and below.

I would suggest you work out how to read the file first. That seems to be more important.

Why are you mixing C++ I/O (cin, cout) with C I/O (fopen, printf, fscanf)??? Your program is quite schizophrenic. It needs a psychiatrist. Decide on what style I/O you want and stick to it.

And get rid of conio.h. You do not need it!

I've tried some things, but still not working. I thought this was going to help:

 cout<<"Enter ID:  ";
 cin>>ID1
 fscanf (in,"%.0f",ID)
 cout<<ID;    -I did this to see what does it read-
      if(ID1==ID)
      {
      ...
      }

I was expecting the output to be the ID. But I get things like:4.50000e-32, 5.2120000e-32

In the file I've the ID In this way: 20912693; genre; name;.... _

I've declared ID and ID1 both as float.

Any suggestion ?

Now why would reading your character string ID using a floating point format help? Please explain. Are you planning on using math on this number?

Keep it a char array -- it's not really a number, it's an ID.

Did this mean anything to you?
Why are you mixing C++ I/O (cin, cout) with C I/O (fopen, printf, fscanf)???
It doesn't look like it.

Step back. What is the format of your input? Is it separate values (separated by SPACEs)? If it's not, you are making it hard to process.

If you need those underscores between the fields, fine. Read the entire line as one string and move the fields one character at a time into the field variables (ID, genre, name...). Be sure to add the '\0' before moving on to the next field.

Print out before, during, after to make sure things are going right.

Edited 4 Years Ago by WaltP

You can use strstr() (searches for a string, inside a string), to search each line of the file, as it's read.

That will hugely simplify your program. Work with code based around logic similar to this:

char *pstr=NULL; 
char line[80];
char target[12]; //plenty of room is good
FILE *fp;
//open the file, and get the string you want to search for, into target[].
//get one line from the file
while((fgets(line, sizeof(line), fp)) != NULL) { // to get each line of the file.
   pstr=strstr(line, target);
   if(pstr) {
      printf("Found it: %s \n",line);
   }
}

C is NOT C++, and you'd be wise to use one or the other. Learning two languages half-way, is not great, and mixing them is a disaster, before long.

Edited 4 Years Ago by Adak

Hello again !, thank you for the replies. I've been quite busy.

Walt, I get it mixed because I was used to handle c++, but with this teacher, I'd to use c, thats why I mixed 'em up a little. I fixed it and Is working so far. I read them as char and use the 'strcmp' to check if both ID were the same, this work so far. But now I have to modify it a little so It can check actually the file to search for an specific ID among others.

That being said, I've to search line by line, until the program localize the ID and print the information with it, or give an error otherwise.

Adak, Thanks for the Info, I was going to start to look for a way to read line by line, you gave me a little boost with It.I'll post the code as soon as I make it work, or atleast where I get stucked.

Thanks for the replies once again. ^^

Well guys, this Is the code so far, Thanks for the help, It was actually very simple, but when you don't know how to do It, Is not so simple xD !!

It Is In spanish, but guess Is quite self explanatory.

void buscar()
{

char  *ID;
char *ID1;
char marca[80];
char busc[12];
char *pstr;
char genre[10];
char name[15];
int age;
float height;


FILE *in;
in=fopen("c:\\Ejercicio.txt","r");

    if (in==NULL)
        perror("Cannot open the file");
    else
{
        clrscr();
        cout<<"Enter ID: ";
        cin>>objt;

    while((fgets(marca, sizeof(marca),in)) !=NULL)
{
        pstr=strstr(marca,busc);

        if(pstr!=NULL)
{
        cout<<"Encontrado \n"<<marca;
getch();

}

}

clrscr();
fclose(in);

}

}

Edited 4 Years Ago by DavdPrez

This article has been dead for over six months. Start a new discussion instead.