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

后端 未结 5 2171
北恋
北恋 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:13

    Since you don't care about the actual content, why bother building a string? I'd also use getchar():

    int    c;
    size_t s = 0;
    
    while ((c = getchar()) != EOF)
    {
      s++;
    }
    
    printf("Size: %z\n", s);
    

    This code will correctly handle cases where your file has '\0' characters in it.

提交回复
热议问题