【问题标题】:C: assigning 2 dimensional array to another arrayC:将二维数组分配给另一个数组
【发布时间】:2018-09-03 05:22:47
【问题描述】:
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;
}

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

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

if (strcmp(temp[x], temp[x + 1]) > 0) 似乎有问题 我不清楚使用指针和地址。

文本文件中的内容是:

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

我希望结果是:

Kitty is team leader in class.

谢谢。

【问题讨论】:

  • 你会在你的顶级代码中添加 cmets 以便更容易理解吗?
  • 不是我的强项,C,但你不是将你的数量 (qty[i]) 分配给 *temp[x]=qty[i];只有,所以 temp[x+1] 没有值或为零/空,因此您的比较返回了意外的结果?
  • 我尝试在运行 strcmp(temp [x], temp [x+1]) > 0 之前添加 if (x>0) 但也不起作用。系统响应:[警告] 从不兼容的指针类型传递“strcmp”的参数 1

标签: c arrays scanf strcmp dimensional


【解决方案1】:

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

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

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

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

  • 您不会为读取的每一行增加 i。每一行输入都会覆盖前一行。

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

  • 您查找数量最多的足球迷的测试已失败:此处不需要二维数组,只需跟踪当前最大值并在需要时更新即可。

这是修改后的版本:

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

【讨论】:

  • @chux:当然!起初我将qty 更改为使用%d,但决定保留OP 的变量。最后一项之后的空白确实在下一个条目的开头结束了第一项...我更改为fgets() 方案删除了​​一些冗余空格。
【解决方案2】:

上面的代码不清楚你想在这个代码块中做什么

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

还有为什么char *temp[200][100];存储qty[i]char *temp就够了,或者你可以拿char temp[200][100];

这里有一个更好的,因为要求不明确。

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

【讨论】:

  • 针对&gt; 0 的测试不能确保goods[i] 被正确填充。最好使用== 3
猜你喜欢
  • 2022-11-27
  • 2011-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 2018-10-12
  • 1970-01-01
相关资源
最近更新 更多