How to read the standard input into string variable until EOF in C?

后端 未结 5 2152
北恋
北恋 2020-12-05 08:44

I am getting \"Bus Error\" trying to read stdin into a char* variable. I just want to read whole stuff coming over stdin and put it fi

5条回答
  •  無奈伤痛
    2020-12-05 09:21

    Assuming that you want to get (shorter than MAXL-1 chars) strings and not to process your file char by char, I did as follows:

    #include 
    #include 
    #define MAXL 256
    
    main(){
      char s[MAXL];
      s[0]=0;
      scanf("%s",s);
      while(strlen(s)>0){
        printf("Size of %s : %d\n",s,strlen(s));
        s[0]=0;
        scanf("%s",s);
      };
    }
    

提交回复
热议问题