c - 从 C 中的多行 STDIN 读取 : segmentation error or no reading at all

标签 c arrays

我正在尝试从标准输入读取多行,其中偶数行是字符串,奇数行是用空格分隔的数字。我正在尝试将数字读取为整数,将字符串读取为...字符串。这是学校项目的一部分,但不是全部;我管理了其余的内容,但我无法真正从标准输入中获取字符串和整数。

当 i 为偶数时,我将每个名称添加到实验中(我尝试将其用作行号) 当我遇到空格时,我尝试使用 malloc 追加字符串 n 并将其作为 int 存储在二维数组数据中,使用 int a 浏览该行。

然后打印部分只是试图展示它的工作原理......但它没有。我没有破坏任何数组的长度,而且我觉得我在留意 malloc,但我在这部分上花了超过 15 个小时,但没有得到任何好处。我想知道是否有人可以给我提示。

 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
int main(int argc, char **argv) {
char *experiments[100];
int data[10][20];
char name[101];
int i=0;
int j=0;
char *n;
char *g;
fgets(name, 100, stdin);
while ((strstr(name,"*** END ***")!=0)&&(name!=NULL)){
    if((i%2)==0){
        experiments[i/2]=name;
        name[0]='\0';
    }
    else {
        int a = 0;
        while ((name[a]!='\n')&&(a<100)){
            if (name[a]!=' '){
                size_t len = strlen(n);
                g = malloc(len + 1 + 1);
                strcpy(g,n);
                g[strlen(n)-2] = name[a];
                g[strlen(n)-1] = '\0';
                n[0]='\0';
                *n = *g;
                free( g );
                a+=1;
            }
            else {
                data[j][i]=*n;
                j+=1;
                n[0]='\0';
                a+=1;

            }
        }
    }
    i+=1;
    fgets(name,100, stdin );
}
int k=0;
for(k=0;k<=i;k+=1){
    printf("printing\n");
    printf("%s\n", experiments[k]);
    if (experiments[k][0]=='\0') {
        printf("oh shoot!");
    }
    }

return(0);}

最佳答案

您似乎对以下方面有根本性的困惑:

  • 你知道这句话吗“给我六个小时砍一棵树,我会用前四个小时磨斧子”?这里有很多问题,很可能是由钝斧造成的。无论你用什么书来学习 C,都无法教你。我推荐K&R 2E。别忘了做练习。
  • 哎呀! forreturn 都不是函数!出于对生活的热爱,如果您希望其他人阅读您的代码,请使其对他们来说美观!如果您的代码也始终缩进,将会有所帮助。
  • 数组(例如,name!=NULL 不可能计算 false,因此该表达式毫无意义)、指针以及实验中发生的从数组到指针的隐式转换[i/2]=名称;。澄清一下,每次这样分配时,不同的元素将指向同一个位置,并且当您下次调用 fgets 时,存储在该位置的值将被覆盖。
  • malloc;您在错误的地方使用了它,并且您使用它的方式重新发明了自动存储持续时间(即所有变量)。您还不如根本不使用它。
  • fgets;它的失败模式会导致您的程序严重崩溃。
  • 字符串;见上文。

首先阅读 K&R 2E 并做我之前提到的练习...一旦您读完这本书,我认为您将有很好的机会填补该程序的空白:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char **argv) {
    char *experiments[100] = { 0 };
    int data[10][20] = { 0 };
    char name[101] = { 0 };
    size_t i = 0, j = 0;
    fgets(name, sizeof name, stdin);
    while (strstr(name,"*** END ***") != 0){
        if(i%2 == 0){
            experiments[i / 2] = malloc(strlen(name) + 1);
            if (experiments[i / 2] == NULL) {
                puts("OOPS! malloc failure!");
                return 0;
            }
            strcpy(experiments[i / 2], name);
        }
        else {
            /* XXX: I have no idea what any of this was meant to do *
             * ...  but it was all HORRIBLY WRONG so I removed it.  *
             *      Try again once you've read K&R 2E and done the  *
             *      exercises...                                    */
        }
        i++;
        fgets(name, sizeof name, stdin);
    }

    for (size_t k = 0; k < i / 2; k++){
        puts("Printing...");
        puts(experiments[k]);
        if (experiments[k][0] == '\0') {
            puts("oh shoot!");
        }
        free(experiments[k]);
    }

    return 0;
}

关于c - 从 C 中的多行 STDIN 读取 : segmentation error or no reading at all,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213212/

相关文章:

c - "open_file:"在c中的含义?

javascript - React 中的条件状态

c++ - 使用 C++ 在 NxN 数组中查找 M 个最大元素的优化方法

c - 如何在 C 中将多个整数值放入 void 数组中?

objective-c - 如何在 Objective-C 中创建数字数组?

javascript - 如何从 NodeList 的索引开始应用样式?

python - 通过循环迭代 numpy 数组的元素

c - 指针数组元素中有错误(不需要的)值

iphone - Objective-C 访问数组

c - 防止浮子除法中的上溢/下溢