如何将结构中的char *值与作为c程序中的输入的char []值进行比较

时间:2016-10-02 08:19:35

标签: c arrays pointers

基本上,我对C编程很陌生。我创建了两个Structs即 项目(用于从带有分隔符的文本文件中检索我的所有项目以供显示)

struct Item
{
    char *itemcd;
    char *itemnm;
    char *desc;
    char *qqun;
};

和Item ID从同一个文件中检索id,但这个用于比较目的

struct ItemID
{
    char *itemcd;
};

这是我进行比较的代码

    FILE *fp = NULL;
    int i = 0;
    struct Item var = { NULL, NULL, NULL, NULL };
    struct ItemID val = { NULL };
    char line[SIZE] = { 0 }, *ptr = NULL;
    char cd[10];

    /* 1. Open file for Reading */
    if (NULL == (fp = fopen("C:\\Users\\Arcraider\\Documents\\Visual Studio 2013\\Projects\\InventoryProject\\inventItems.txt", "r")))
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);
    }

    /* 2. Allocate Memory */
    var.itemcd = (char*)malloc(SIZE);
    var.itemnm = (char*)malloc(SIZE);
    var.desc = (char*)malloc(SIZE);
    var.qqun = (char*)malloc(SIZE);

    printf("=============================Purchase Items Available=======================================\n\n");
    /* 3. Read each line from the file */
    while (EOF != fscanf(fp, "%s", line))
    {
        /* 4. Tokenise the read line, using ":" delimiter*/
        ptr = strtok(line, ":");
        var.itemcd = ptr;

        while (NULL != (ptr = strtok(NULL, ":")))
        {
            i++;
            /* 5. Store the tokens as per structure members , where (i==0) is first member and so on.. */
            if (i == 1)
                var.itemnm = ptr;
            else if (i == 2)
                var.desc = ptr;
            else if (i == 3)
                var.qqun = ptr;
        }

        i = 0;        /* Reset value of i */
        printf("Item Code: [%s] Item Name: [%s] Description: [%s] Quantity: [%s]\n", var.itemcd, var.itemnm, var.desc, var.qqun);

    }
    fclose(fp);

    printf("\n\n============================================================================================\n\n");
    printf("Enter the item code which you would like:");
    fflush(stdin);
    scanf("%s", &cd);

    //validate data entry for id
    if (NULL == (fp = fopen("C:\\Users\\Arcraider\\Documents\\Visual Studio 2013\\Projects\\InventoryProject\\inventItems.txt", "r")))
    {
        perror("Error opening the file.\n");
        exit(EXIT_FAILURE);
    }

    /* 2. Allocate Memory */
    val.itemcd = (char*)malloc(SIZE);


    /* 3. Read each line from the file */
    while (EOF != fscanf(fp, "%s", line))
    {
        /* 4. Tokenise the read line, using ":" delimiter*/
        ptr = strtok(line, ":");
        val.itemcd = ptr;

        while (NULL != (ptr = strtok(NULL, ":")))
        {
            i++;
            /* 5. Store the tokens as per structure members , where (i==0) is first member and so on.. */
        }
        i = 0;        /* Reset value of i */
        printf("\nItem Code: %s\n", val.itemcd);
        if (cd == val.itemcd){
            printf("Code Match Successful!!!");
            break;
            _getch();
        }
        else if (cd != val.itemcd)
        {
            printf("Matching......Code not present. Failed\n");
        }
    }
    _getch();

运行代码时,我不断得到一个"匹配......代码不存在的结果。失败"即使我输入正确的值" K1234"用于检测。请帮我做比较部分。

错误的图像是:

enter image description here

0 个答案:

没有答案