Usage of fgets function in C

后端 未结 7 1607
野性不改
野性不改 2020-12-19 17:51

One of my assignments in to write my own UNIX Shell. To receive input from the user, I am using fgets to capture the input as a string but I\'m not really sure how it works.

7条回答
  •  攒了一身酷
    2020-12-19 18:29

    From the fgets manual page:

    fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer.

    Bottom-line: you have an extra newline at the end of your string when comparing.

提交回复
热议问题