C:将二维数组分配给另一个数组

C:将二维数组分配给另一个数组,c,arrays,scanf,strcmp,dimensional,C,Arrays,Scanf,Strcmp,Dimensional,大家好,我不知道为什么结果不正确 我想知道谁有更多的足球,并打印出他/她的名字 如果(strcmp(temp[x],temp[x+1])>0) 我不清楚如何使用指针和地址 文本文件中的内容包括: int main() { FILE *fp = fopen("fileA.txt", "r"); /* read file */ int i = 0; char name[200][100]; char goods[200][100]; char qty[200]

大家好,我不知道为什么结果不正确

我想知道谁有更多的足球,并打印出他/她的名字

如果(strcmp(temp[x],temp[x+1])>0) 我不清楚如何使用指针和地址

文本文件中的内容包括:

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;
}
我希望结果是:

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

谢谢。

以上代码不清楚,您想在此代码块中执行什么操作

Kitty is team leader in class.
还有为什么
char*temp[200][100]
对于存储
qty[i]
char*temp
就足够了,或者您可以选择
char temp[200][100]

这是一个更好的方法,因为要求不明确

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

您的代码中存在多个问题:

  • 不测试文件是否正确打开

  • 当(!feof(fp)){
时,您无法正确解析文件。
只要
fscanf()
返回3,您就应该迭代,或者最好逐行读取输入并使用
sscanf()
对其进行解析

  • 您没有告诉
    fscanf()
    要存储到目标数组中的最大字符数。这可能会导致无效输入的未定义行为

  • 您不会为每一行读取递增
    i
    。每一行输入都会覆盖上一行

  • 您不检查是否有超过200行。在这种情况下,未定义行为

  • 找到数量最高的球迷的测试失败了:这里不需要2D数组,只需跟踪当前的最大值,并在需要时更新它

  • 以下是修改后的版本:

    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;
    }
    
    #包括
    int main(){
    FILE*fp=fopen(“fileA.txt”,“r”);/*读取文件*/
    char-buf[300];
    字符名[200][100];
    炭制品[200][100];
    字符数量[200][100];
    整数i,数量,最大数量=0,结果=-1;
    如果(fp==NULL){
    fprintf(stderr,“无法打开文件”\n);
    返回1;
    }
    对于(i=0;i<200;i++){
    如果(!fgets(buf,sizeof buf,fp))
    打破
    如果(sscanf(基本单位),%99[^,],%99[^,],%99s],名称[i],项目[i],数量[i])!=3){
    fprintf(标准,“无效输入:%s\n”,buf);
    打破
    }
    如果(strcmp(项目[i],“Football”)==0{/*仅选择Football*/
    数量=原子(数量[i]);
    如果(结果==-1 | |数量>最大数量){
    结果=i;/*存储拥有更多足球的人的索引*/
    }
    }
    }
    如果(结果<0)
    printf(“根本没有球迷!\n”);
    其他的
    printf(“%s是班上的队长,有%d个足球队。\n”,名称[结果],最大数量);
    fclose(fp);
    getchar();
    返回0;
    }
    
    你会在你的顶层代码中添加注释以便更容易理解吗?这不是我的强项C,但你不是将你的数量(数量[i])分配给*temp[x]=qty[i];只是,这样temp[x+1]没有值或为零/空,因此你的比较会返回意外的结果吗?在运行strcmp(temp[x],temp[x+1])之前,我尝试添加if(x>0)>0但也不起作用。系统响应:[警告]通过针对
    >0
    的不兼容指针类型测试中的'strcmp'参数1,不会为
    货物[i]投保
    已正确填充。最好使用
    ==3
    @chux:当然!起初我将
    qty
    更改为使用
    %d
    ,但决定保留OP的变量。最后一项之后的空白确实会在下一项的开始处结束第一项…我改为
    fgets()
    scheme删除了一些冗余空间。
    #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;
    }