Difference between fgets and gets

后端 未结 4 1960
面向向阳花
面向向阳花 2021-01-21 03:59

What is the difference between fgets() and gets()?

I am trying break my loop when the user hits just \"enter\". It\'s working well with

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-21 04:53

    you can use fgets() with STDIN instead. This function is secured and always insert a '\0' at the string end.

    An example:

    char inputbuffer[10];
    char *p;
    p = fgets(inputbuffer, sizeof(inputbuffer), stdin);
    printf(">%s<\n", p);    /* p is NULL on error, but printf is fair */
    

    You'll get at most 9 characters + '\0', in this example.

提交回复
热议问题