Search code examples
carraysscanfstrcmpdimensional

C: assigning 2 dimensional array to another array


int main() {
    FILE *fp = fopen("fileA.txt", "r"); /* read file */
    int i = 0;
    char name[200][100];
    char goods[200][100];
    char qty[200][100];
    char temp[200][100];
    int x = 0;
    int result;

    while (!feof(fp)) {
        fscanf(fp, "%[^,] , %[^,] , %s " , name[i], item[i], qty[i]); /*get file content and store in array */ 

        if (strcmp(item[i], "Football") == 0) { /* only select Football */
            temp[x][x] = qty[i];
            if (x > 0) {
                if (strcmp(temp[x][x], temp[x + 1][x + 1]) > 0) { /*compare who has more football qty */
                    result = x; /*output the person who have more football*/
                }   
            }
            x = x + 1;
        }
    }

    printf("%s is team leader in class.\n", name[result]);

    fclose(fp);
    getchar();
    return 0;
}

Hi all, I don't know why the result not correct.

I want to find out who has more football and print out his/her name.

Seems something wrong on if (strcmp(temp[x], temp[x + 1]) > 0) I am not clearly on using pointer and address.

the content in the text file are:

Alice,Eating,001
Kitty,Football,006
Ben,Swimming,003
May,Football,004

And I expect the result is :

Kitty is team leader in class.

Thank you.


Solution

  • There are multiple problems in your code:

    • you do not test if the file is properly open.

    • you cannot properly parse a file with while (!feof(fp)) {. You should iterate for as long as fscanf() returns 3, or preferably read the input line by line and parse it with sscanf().

    • you do not tell fscanf() the maximum number of characters to store into the destination arrays. This may cause undefined behavior for invalid input.

    • you do not increment i for each line read. Every line of input overwrites the previous one.

    • you do not check if there are more than 200 lines. Undefined behavior in this case.

    • Your test to find the football fan with the highest quantity is broken: no need for a 2D array here, just keep track of the current maximum and update it when needed.

    Here is a modified version:

    #include <stdio.h>
    
    int main() {
        FILE *fp = fopen("fileA.txt", "r"); /* read file */
        char buf[300];
        char name[200][100];
        char goods[200][100];
        char qty[200][100];
        int i, qty, max_qty = 0, result = -1;
    
        if (fp == NULL) {
            fprintf(stderr, "cannot open file\n");
            return 1;
        }
        for (i = 0; i < 200; i++) {
            if (!fgets(buf, sizeof buf, fp))
                break;
            if (sscanf(buf, " %99[^,], %99[^,],%99s", name[i], item[i], qty[i]) != 3) {
                fprintf(stderr, "invalid input: %s\n", buf);
                break;
            }
            if (strcmp(item[i], "Football") == 0) { /* only select Football */
                qty = atoi(qty[i]);
                if (result == -1 || qty > max_qty) {
                    result = i; /*store the index of the person who have more football */
                }
            }
        }
    
        if (result < 0)
            printf("no Football fan at all!\n");
        else
            printf("%s is team leader in class with %d in Football.\n", name[result], max_qty);
    
        fclose(fp);
        getchar();
        return 0;
    }