尝试使用strtok从文件中分离字符串并将其存储在结构数组中

问题描述

我已经编写了以下代码

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\n",c.name,c.age,c.phoneNo,c.homeAddress.houseNumber);
        }
        if (!foundRecord)
            printf("%s cannot be found\n",search);
    }
    fclose(fPtr);
}

当我在主体中调用void搜索功能时,代码运行不正常(我无法输入要搜索的任何内容,并且得到了一堆毫无意义的符号)。基本上,我有一个名为customer的结构,我将其构造为一个结构数组,并且试图输入客户的详细信息(例如其姓名),以搜索其详细信息,将其存储到该结构数组中并显示出来。客户详细信息存储在如下文本文件中:

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

我可以知道如何解决我的代码以使其起作用吗?

解决方法

要详细说明该函数如何在不使用数组的情况下可以工作,它可能类似于:

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\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);
}