Thread: Searching in a file

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    Searching in a file

    I am making a phone book, and this code is suppose to b used to search for the name in the file, but it keeps saying contact not found, even if the name exist in the file.. please help?



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 300
    struct contact
    {
    char lastName [30] ;
    char firstName [30];
    char adress [100];
    char city [25];
    char phone [20];
    };
    int main()
    {
    struct contact new1[SIZE];
    FILE *open;
    int i=0,k;
    int x,j=0;
    char ln[30];
    char c;
    char *ptr1;
    char *ptr2;
    int flag=0, anotherFlag = 0;
    open = fopen("C:\\Users\\Roumia\\Desktop\\contacts.txt","r");
    if (open == NULL)
    {
    printf("file doesn't exist !\n");
    }
    else{
    printf("file exist !\n");
    while(!(feof(open)))
    {
    fscanf(open,"%[^,],%[^,],%[^,],%[^,],%s",new1[i].lastName,new1[i].firstName,new1[i].adress,new1[i].city,new1[i].phone);
    fscanf(open,"\n");
    i++;
    }
    }
    printf("Please enter the last name of the person you need to find: ");
    while((c=getchar())!='\n' && j<30)
    {
    ln[j++]=c;
    }
    ln[j] = '\0';
    ptr1=ln;
    for(k=0;k<i;k++){
    ptr2=new1[k].lastName;
    ptr1 = ln;
    for(x=0;x<j;x++)
    {
    if(*ptr1 != *ptr2)
    {
    flag = 1;
    }
    ptr1++;
    ptr2++;
    }
    if(flag==0)
    {
    anotherFlag = 1 ;
    printf("Contact was found !!\n");
    printf("%d\n",k);g
    printf("%s,%s,%s,%s,%s",new1[k].lastName,new1[k].firstName,new1[k].adress,new1[k].city,new1[k].phone);
    }
    }
    if(anotherFlag==0)
    {
    printf("Contact wasn't found !!\n");
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    I think the problem is here

    Code:
    // i: number of stored records
    // ln: last name string which is to be found
    // j: strlen(ln)
    // flag: has an initial value of 0
    // anotherFlag: has an initial value of 0
    
    ptr1=ln;
    for(k=0;k<i;k++){
        ptr2=new1[k].lastName;
        ptr1 = ln;
        for(x=0;x<j;x++)
        {
            if(*ptr1 != *ptr2)
            {
                flag = 1;
            }
            ptr1++;
            ptr2++;
        }
        if(flag==0)
        {
            anotherFlag = 1 ;
            printf("Contact was found !!\n");
            printf("%d\n",k);g
            printf("%s,%s,%s,%s,%s",new1[k].lastName,new1[k].firstName,new1[k].adress,new1[k].city,new1[k].phone);
        }
    }
    When flag==0, you mean that a contact was found. But notice that flag will be set to 1 as soon as two of the characters do not match, and it will never be reset to 0 in case a match is found in another record.

    Rather than do it like this, why not use a standard function like strcmp to do the string comparison? Same goes for reading the "ln" from the user. When a built-in function will do the job, it's better to use it. When not, it's better to write a function to do the job. That would keep your logic more high-level and easier to follow.

    Also it helps to post the complete input that you are using. We can only guess what you are using as your input file data.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    i'm nt suppose to use functions in this assigment :S i dnt quite get u, can u b more clear please?

    Quote Originally Posted by c99tutorial View Post
    I think the problem is here

    Code:
    // i: number of stored records
    // ln: last name string which is to be found
    // j: strlen(ln)
    // flag: has an initial value of 0
    // anotherFlag: has an initial value of 0
    
    ptr1=ln;
    for(k=0;k<i;k++){
        ptr2=new1[k].lastName;
        ptr1 = ln;
        for(x=0;x<j;x++)
        {
            if(*ptr1 != *ptr2)
            {
                flag = 1;
            }
            ptr1++;
            ptr2++;
        }
        if(flag==0)
        {
            anotherFlag = 1 ;
            printf("Contact was found !!\n");
            printf("%d\n",k);g
            printf("%s,%s,%s,%s,%s",new1[k].lastName,new1[k].firstName,new1[k].adress,new1[k].city,new1[k].phone);
        }
    }
    When flag==0, you mean that a contact was found. But notice that flag will be set to 1 as soon as two of the characters do not match, and it will never be reset to 0 in case a match is found in another record.

    Rather than do it like this, why not use a standard function like strcmp to do the string comparison? Same goes for reading the "ln" from the user. When a built-in function will do the job, it's better to use it. When not, it's better to write a function to do the job. That would keep your logic more high-level and easier to follow.

    Also it helps to post the complete input that you are using. We can only guess what you are using as your input file data.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What he's suggesting, is that when the first name fails, and flag is assigned the value of 1, - so the name you're looking for is not the first name in the array of structs - but now, the flag is not reset to 0. Without that flag being reset, the search will never find a name.

    So reset the flag, at the end (or beginning), of every time you increment k and start looking at a new name.

    Indenting your code will go a long way to making your program a lot easier to study.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    while(!(feof(open)))
    {
    fscanf(open,"%[^,],%[^,],%[^,],%[^,],%s",new1[i].lastName,new1[i].firstName,new1[i].adress,new1[i].city,new1[i].phone);
    fscanf(open,"\n");
    i++;
    }
    Don't use end-of-file test to control your loops. It is seldom used/done correctly. For your purposes, I would suggest replacing the feof test with a call to fgets to get the line from the file into a temp buffer. Then, within the loop change fscanf to scanf to do your parsing on the temp buffer.

    The link posted contains examples both flawed "while(!feof..." and correct "while(fgets...".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by sarahradi View Post
    i'm nt suppose to use functions in this assigment :S i dnt quite get u, can u b more clear please?
    seems like you are familiar with pointers though so I'm assuming you've also done functions. If so, consider making your program more modular and creating functions to find the given string and such. This will also go a long way to making your code more readable.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching a file
    By na_renu in forum C Programming
    Replies: 7
    Last Post: 01-21-2006, 05:01 PM
  2. Searching in a file
    By bliss in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2005, 09:35 PM
  3. File I/O searching...
    By kjmagic in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2005, 12:30 PM
  4. Searching a .txt file?
    By Vladk10000 in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2004, 11:07 AM
  5. File searching
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-24-2003, 09:00 PM

Tags for this Thread