I am working with a code which stores the information of customers in a file called customer_details.txt
then asks the user to enter a name and then searches for the name in customer_details.txt, if the name is found, the entire details of the user is displayed on the output screen.

here is what I have done:

void main()
FILE *fp;
char name[20],dob[12],add[50];
int acc_no,i=0,random=95867;
double mob_no;
    cout<<"Error opening file customer_details.txt !!";

cout<<"\n\nEnter name:";

cout<<"\n\nEnter Date of Birth:(Date Month and Year separated by `-' ex: 25-01-1994)\n:";
cout<<"\nEnter mobile number:";
cout<<"Enter address:\n";
fprintf(fp,"\n\n\tUser ID: %d",i+1);
fprintf(fp,"\n\t Account number: %d",acc_no);
fprintf(fp,"\n\t  Name: %s",name);
fprintf(fp,"\n\t Date of Birth: %s",dob);
fprintf(fp,"\n\t Mobile no.: %lf",mob_no);
fprintf(fp,"\n\t Address: %s ",add);

where I need help is how to search for that particular name in the file.

thanx in advance..

Hello i designed the code & it is working fine but i am unable to post it .It shows the formatted code in code snippet is not proper pls help me.

    char * my_strstr(char *,char *);
    int main()
            FILE *fp;
            char record[100],ch;//record is the array to read  one record (Details of one person)
            char *a=NULL;//To know the status
            int i;
            char name[20];//IT STORES THE NAME WHICH U WILL ENTER TO QUERY
            printf("Enter your namen");
                    printf("ERROR IN OPENING THE FILEn");
                    if(a!=NULL)//if found go out of the loop
                            printf("Record Foundn");  
            if(a==NULL)//IF NOT FOUND
                    printf("Record not FoundnTry Again Pleasen");
        return 0;

    char*  my_strstr(char *p,char *q)
            int found=0,k=0,i=0,j=0;
                    for(j=i,k=0;p[j]==q[k] && q[k]!='�';j++,k++);
                            return p;
            return NULL;

Hope it is clear to u.If any query please ask.
Thanks for the question.i m really happy i did it.

Suppose i m taking consideration that each record is separated by a line
From your code it is clear that each details(like name,DOB) is separated by '\t'.& each record (details of one person)separated by '\n'.
It means 

sachi   12345   M       60000009009
sahu    1233    F       76578
happy   6456    M       34536363636

So i have designed code according to like this,its vary simple i just implemented my own strstr named"my_strstr()".U check it out.As u designed to write new record in to the file.I searches name & when it matches then it gives the total details about that person.

Note The some '?' it is showing is '\0'.
Hope it is clear to u.If any query please ask.
Thanks for the question.i m really happy i did it.

First, be aware that your code is C++, and you are compiling it (obviously) with a C++ compiler, instead of the C compiler.

This answer works in C, but you may need to adapt it to work with C++. Sachi's code is also C, since this is the C forum.

If this is the way your data is laid out:

sachi 12345 M 60000009009
sahu 1233 F 76578
happy 6456 M 34536363636

Then the simple way to find a name is to use code similar to this:

First, you need to #include <string.h>, so you can use strstr().

char *ptr;
char target[120]; //over-sized is good
printf("Name to search for: ");
char oneLine[120];    // ditto
while((fgets(oneLine, 120, theFilePointerName)) != NULL) {
   ptr = strstr(oneLine, target);
   if(ptr) {
      printf("Customer was found\n%s\n",oneLine);

if(!found) {
   printf("Customer was not found\n");

No one should be using gets() anymore, btw. Way too unsafe (and has been deprecated). If the data is in the proper record format, you should be using that, or working with the data based on entire rows at a time, not working with getchar(), in my judgement.

You'll find working with C much easier if you stop using your C++ compiler and asking questions about C++ code, in the C forum. ;)

You will want to adjust the code to your needs - maybe stop the search when the name is first found, perhaps use sscanf() on oneLine to put the record into the struct, etc.

Yes sir thanks for yor words...it will really motivate me...i am new to this sir...From next time i will be taking care of this

You have an earnest attitude, Sachi. Keep it up, and it will carry you far.

Hello I need help to make library management system in which these function are included:-
First make structure of library .....To get
Book no
Book title
Author name
Year published
Isbn no
2nd are operations....... to perform
Search by book no, isbn no, book title and by author name
calculate total no of books...
Plz plz some buddy help me
My name is Muhammad Gohar and
My email is goharsarwar@gmail. com

Hi Muhammad_112 ,

You should start a new thread instead of using an old one opened by someone else about 4 years back.

Then don't just tell what you want done. Show some work that you have been able to on your own, and where exactly the help is needed, expect of course you are paying for the services of someone writing your code for you.
Even at that, you will not be doing yourself any good.