Thread: search funtion driving me mad

  1. #1
    Registered User
    Join Date
    Mar 2004

    search funtion driving me mad

    The user enters a name and then the program compares the entry with designer[num_of_items1_i].forename_s and prints the name if the strcmp ==0 but it never seems to find the name? Ive looked at the code but cant see why it doesnt work. Any ideas?

    void f_search_designer ()
       int ch;
        char findname_s[20];
        FILE *fp;
        (fp = fopen ("designers", "rb" ));
        if ((fp = fopen ("designers", "rb" )) == NULL)
            printf("\n\t\tCannot open file\n");
            fread( &designer, sizeof( DESIGNERRECORD ), 1, fp);
            printf("        ____________________SEARCH______________________\n\n");
            printf("\t\tEnter the name of the designer you would like to search > \n\n");
            scanf ("%s", &findname_s);
            if (strcmp (findname_s, designer[num_of_items_i].forename_s ) == 0)
                printf("NAME        \n");
                fread ( &designer, sizeof( DESIGNERRECORD ), 1, fp );
                printf ("\n\n\n\n\t\tThere are no matching names.\n\n\n\n");

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    fread( &designer, sizeof( DESIGNERRECORD ), 1, fp);
    You realize you're only reading one instance here, don't you? Yet here:
    if (strcmp (findname_s, designer[num_of_items_i].forename_s ) == 0)
    you're trying to use an array of said items. To simplify issues for you, try not using a pointer first, and just create an instance, read one, test it. See if that works:
    struct myrecord instance;
    fread( &instance, sizeof( struct myrecord ), 1, myfile );
    if( strcmp( nametofind, ) == 0 )
    You may want to also try printing the contents of the name variable that you've just read out first, to see what it is you're acutally reading. Keep in mind that strcmp is case sensitive.

    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. need help writing search funtion
    By drharv in forum C Programming
    Replies: 6
    Last Post: 02-24-2002, 01:04 PM