Thread: Searching a file in C

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    22

    Searching a file in C

    Hello there, I am trying to search a file in C using a phone number. Records are stored in this format:
    1-###-###-### x###|Name|Address|false

    Now the real issue. Say these two records are in the file
    (564) 412-0388|Rodriguez, Rodriguez and Rodriguez|Suite 287 1609 Hayes Rue, Ernesthaven, OK 96769-8419|false
    (564) 154-2439 x464|Kilback Group|041 Horace Cliff, O'Connermouth, WV 69929-2122|true


    Notice they have the same code (564). The program when scanning from the file doesnt scan the entire number. It just stops when it scans (564). My code uses linked lists and hashing.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #define MAX 1000
    
    
    
    
    //Create a struct for the linked list
    typedef struct phonebook {
        char phoneNumber[31];
        char code[50];
        char name[50];
        char address[100];
        char status[500];
        struct phonebook *next;
    } Phonebook;
    
    
    //declare hash table
    Phonebook* hashTable[MAX];
    
    
    //Function to create the linked list
    void createList(){
        FILE *fp;
        char line[200];
        fp = fopen("phonebook.txt", "r");              //opens the file in read mode and scans every line
        while(fgets(line, 200, fp)){
            Phonebook *temp = (Phonebook*)malloc(sizeof(Phonebook));     //create temporary node
            sscanf(line, "%s %[^|]| %[^|]| %[^|]|", temp->phoneNumber, temp->code, temp->name, temp->address);   //scan all the information from each line
            temp->next = NULL;
    
    
            int hash_value = abs(atoi(temp->phoneNumber)) % MAX;
            if(hashTable[hash_value] == NULL) {      //if the hash value is null we store the number in temp variable
                hashTable[hash_value] = temp;
            }
            else {
                Phonebook *head = hashTable[hash_value];   //else store at head
                while (head->next != NULL) {
                    head = head->next;
                }
                head->next = temp;
            }
        }
        fclose(fp);
    }
    
    
    //Function to search for a number
    void searchNumber(char *number){
        int hash_value = abs(atoi(number)) % MAX;
        Phonebook *temp = hashTable[hash_value];
        while (temp != NULL) {
            if (strcmp(temp->phoneNumber, number) == 0) {   //compare the two if the same return 0 and print info
                printf("Number: %s\nName: %s\nAddress: %s\n", temp->phoneNumber, temp->name, temp->address);
                return;
            }
            temp = temp->next;
        }
        printf("Number not found in the phonebook\n");
    }
    
    
    // input function
    void input(){
        char input[15];
        printf("Enter the phone number to search: ");
        scanf("%s", input);
        searchNumber(input);
    }
    
    
    int main(){
        createList();
        input();
        char answer[3];
    }
    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Notice they have the same code (564)
    The first thing I notice is that the data isn't in the format that you've specified.
    Remember that %s stops as soon as it encounters a space.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    May 2022
    Posts
    22

    Reply

    Quote Originally Posted by john.c View Post
    The first thing I notice is that the data isn't in the format that you've specified.
    Remember that %s stops as soon as it encounters a space.
    Hey John. I get this but I don't understand what format operator do I replace it with?

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    You probably need to use the same kind of format as the others to read the entire field and then extract the code if it's present.
    Below I've also tried to limit the amount that can be read into each field appropriately (and I've changed some sizes).
    Note that you can also add the new node to the front of the list, which is easier.
    Code:
    typedef struct phonebook {
        char phoneNumber[20];
        char code[10];
        char name[50];
        char address[100];
        char status[10];
        struct phonebook *next;
    } Phonebook;
     
    void createList() {
        char line[200];
        FILE *fp = fopen("phonebook.txt", "r");
        while (fgets(line, 200, fp)) {
            Phonebook *temp = (Phonebook*)malloc(sizeof(Phonebook));
            sscanf(line, " %19[^|]| %49[^|]| %99[^|]| %9s",
                temp->phoneNumber, temp->name, temp->address, temp->status);
            temp->next = NULL;
     
            // Extract code if present
            temp->code[0] = '\0';
            char *p = strchr(temp->phoneNumber, 'x');
            if (p) {
                strncat(temp->code, p, 9);
                if (p[-1] == ' ') --p;
                *p = '\0';
            }
     
    //        printf("[%s] [%s] [%s] [%s] [%s]\n", temp->phoneNumber, temp->code,
    //            temp->name, temp->address, temp->status);
     
            int hash_value = abs(atoi(temp->phoneNumber)) % MAX;
            temp->next = hashTable[hash_value];
            hashTable[hash_value] = temp;
        }
        fclose(fp);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start with some practice using the format.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        char line[] = "(564) 412-0388|Rodriguez, Rodriguez and Rodriguez|Suite 287 1609 Hayes Rue, Ernesthaven, OK 96769-8419|false";
        char words[4][100] = { { 0 } };
        int n = sscanf(line, "%s %[^|]| %[^|]| %[^|]|", words[0], words[1], words[2], words[3]);
        printf("n=%d (should be 4)\n", n);
        for ( int i = 0 ; i < 4 ; i++ ) {
            printf("%d: >>%s<<\n", i, words[i]);
        }
    }
    Do you always get what you expect from each field?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2022
    Posts
    22

    Searching not working now

    So the format of which the program reads the file is now correct. Thank you so much. Here's how it looks:

    [(564) 412-0388] [] [Rodriguez, Rodriguez and Rodriguez] [Suite 287 1609 Hayes Rue, Ernesthaven, OK 96769-8419] [false]


    So I've added a record in the text file to see if the searching still works.
    552-957-9835 x980|Parisian, Parisian and Parisian|Apt. 454 3645 Vance Hollow, Anaisside, DE 72068|false

    So I searched the number and I got this result:
    Number: 552-957-9835
    Name: Parisian, Parisian and Parisian
    Address: Apt. 454 3645 Vance Hollow, Anaisside, DE 72068
    Code: x980


    But when I search the record with the code at the front (564) it doesn't work. I'm not sure why.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    But when I search the record with the code at the front (564) it doesn't work. I'm not sure why.
    Why would you expect that to work? You compare the search string to the entire phone number.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  8. #8
    Registered User
    Join Date
    May 2022
    Posts
    22

    I don't understand

    I don't understand, how I understand is that the program is splitting this number in two because of the space after the area code.
    (564) 412-0388

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    I changed from %s, which stops at spaces, to %[^|], which doesn't, so the area code is not split off. Even if it was, where would it be stored? You have no variable for it. And where are you telling the code to look at the area code and not the phone number?
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching in a file
    By sarahradi in forum C Programming
    Replies: 5
    Last Post: 12-26-2012, 06:51 PM
  2. Searching in a file
    By bliss in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2005, 09:35 PM
  3. Searching a .txt file?
    By Vladk10000 in forum C++ Programming
    Replies: 2
    Last Post: 05-25-2004, 11:07 AM
  4. Searching in file
    By Spedge in forum C Programming
    Replies: 2
    Last Post: 08-16-2003, 04:25 PM
  5. File Searching
    By phantobrain in forum C++ Programming
    Replies: 11
    Last Post: 06-15-2003, 07:06 PM

Tags for this Thread