Search code examples
cstructstructurevoidstrtok

Trying to use strtok to separate string from a file and store in structure array


i've written the code below:

struct customer {
    char name[30]
    int age, phoneNo;
    struct address {
        int houseNumber;
    } homeAddress;
};


void search()
{
    char search[50];
    char record[100];
    const char s[2] = ",";
    char* pStr;
    struct customer c;

    FILE* fPtr;
    fPtr = fopen("customer.txt", "r");
    // flag to check whether record found or not
    int foundRecord = 0;
    printf("Enter name to search : ");
    // fgets gets name to search
    scanf("%s", search);
    //remove the '\n' at the end of string

    while (fgets(record, 100, fPtr))
    {
        // strstr returns start address of substring in case if present
        if (strstr(record, search))
        {
            char* pStr = strtok(record, ",");
            if (pStr != NULL) {
                strcpy(c.name, pStr);
            }
            pStr = strtok(NULL, ",");
            if (pStr != NULL) {
                c.age = atoi(pStr);
            }
            pStr = strtok(NULL, ",");
            if (pStr != NULL) {
                c.phoneNo = atoi(pStr);
            }
            pStr = strtok(NULL, ",");
            if (pStr != NULL) {
                c.homeAddress.houseNumber = atoi(pStr);
            }
            foundRecord = 1;
            printf("Your details: ");
            printf("%s, %d, %d, %d\n", c.name, c.age, c.phoneNo, c.homeAddress.houseNumber);
        }
        if (!foundRecord)
            printf("%s cannot be found\n", search);
    }
    fclose(fPtr);
}

When I call for the void search function in my main, the code isn't working well (i can't input anything to search and i get a bunch of symbols that don't make sense). Basically, I have a struct called customer, I made it a struct array, and I am trying to input a customer's details such as their name, to search for their details, store it into the struct array, and display it. The customer details are stored in a text file like below:

John Doe,10,123456789,5,Streets,Paris,P,60393
Mary Ann,39,12935837,5,Streetss,Cities,C,48354

May I know how I can fix my code so that it works?


Solution

  • To elaborate on how the function could work without the array, it could look something like:

    void search()
    {
        char search[30];  // Match the length of the name in the structure
        char record[100];
        struct customer c;
    
        FILE* fPtr;
        fPtr = fopen("customer.txt", "r");
        // TODO: Should really check for failure here
    
        printf("Enter name to search : ");
        scanf(" %29[^\n]", search);  // Read at most 29 characters, plus the string terminator makes 30
    
        // Read lines from the file
        while (fgets(record, sizeof record, fPtr))
        {
            // strstr returns start address of substring in case if present
            if (strstr(record, search))
            {
                // Found the record we're searching for
    
                char* pStr = strtok(record, ",");
                if (pStr != NULL) {
                    strcpy(c.name, pStr);
                }
                pStr = strtok(NULL, ",");
                if (pStr != NULL) {
                    c.age = atoi(pStr);
                }
                pStr = strtok(NULL, ",");
                if (pStr != NULL) {
                    c.phoneNo = atoi(pStr);
                }
                pStr = strtok(NULL, ",");
                if (pStr != NULL) {
                    c.homeAddress.houseNumber = atoi(pStr);
                }
    
                printf("Your details: ");
                printf("%s, %d, %d, %d\n", c.name, c.age, c.phoneNo, c.homeAddress.houseNumber);
    
                // There's no need to read more data from the file
                // So break out of the loop
                break;
            }
        }
        fclose(fPtr);
    }