简体   繁体   中英

Reading from multi-lines STDIN in C : segmentation error or no reading at all

I am trying to read multiple lines from stdin, in which even lines are strings and odd lines are numbers separated by spaces. I'm trying to read the numbers as integers and the strings as... strings. It's part of a school project but it's not the entire thing; I managed the rest of the stuff but I can't manage to actually GET the strings and ints from stdin.

I add every name to experiments when i is even (I try to use it as a line number) I tried using malloc to append a string n and store it as an int in aa 2d array data when I encounter a space, using int a to navigate through the line.

And then the printing part is just to try to show it works and.. it doesn't. I'm not busting any array's length and I felt like I watched out for malloc but I spent more than 15 hours on this part and nothing good is coming out of it. I wondered if someone could give me a hint.

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

You seem to have fundamental confusions regarding:

  • Do you know the saying "Give me six hours to chop down a tree and I will spend the first four sharpening the axe"? There are many problems here, and they're probably caused by a blunt axe. Whatever book you're using to learn C is failing to teach you. I recommend K&R 2E. Don't forget to do the exercises.
  • Yuck! Neither for nor return are functions! Please, for the love of life, if you want other people to read your code, make it presentable for them! It would help if your code were consistently indented, too.
  • Arrays (eg it's impossible for name!=NULL to evaluate false, so that expression is pointless), pointers and the implicit conversion from array to pointer that occurs in experiments[i/2]=name; . To clarify, every time you assign like that, the different elements will point to the same place, and the values stored within that place will be overwritten when you next call fgets .
  • malloc ; you've used it in the wrong place, and the way you used it reinvents automatic storage duration (that is, all of your variables). You might as well just not use it at all.
  • fgets ; its mode of failure leads to horrible crashes in your program.
  • Strings; see above.

Start by reading K&R 2E and doing the exercises as I mentioned earlier... Once you've completed that book I reckon you'll have a fine chance at filling in the blanks for this program:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM