Cc Li
Cc Li

Reputation: 39

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.

Upvotes: 1

Views: 99

Answers (2)

chqrlie
chqrlie

Reputation: 144770

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

Upvotes: 1

Achal
Achal

Reputation: 11921

Above Code is not clear as what you want to do in this code block

if ( strcmp(temp [x], temp [x+1]) > 0 ){ /* when matches, accessing temp[x+1] results in undefined behaviour */
                result = x;
}

also why char *temp[200][100]; as to store qty[i], char *temp is enough or you can take char temp[200][100];

Here is somewhat better one as requirement is not clear.

int main() {
        FILE *fp= fopen("fileA.txt","r"); /* read file */
        if(fp == NULL) {
                /* not exist.. write something ?? */
                return 0;
        }
        char name [200][100],goods[200][100],qty[200][100],temp[200][100];
        int x = 0,result = 0, i = 0;

        while ((fscanf(fp, "%[^,] , %[^,] , %s " , name[i], goods [i], qty[i])) == 3) {
                if (strcmp(goods[i] , "Football") == 0){
                        strcpy(temp[x],qty[i]);
                        if ( strcmp(temp [x], temp [x+1]) > 0 ) { /* UB ? */
                                result = x;
                                x+=1;
                        }
                }
        }
        printf("%s is team leader in class. \n", name[result]);
        fclose(fp);
        getchar();
        return 0;
}

Upvotes: 0

Related Questions