read() from stdin doesn't ignore newline

前端 未结 5 872
情话喂你
情话喂你 2020-12-10 21:01

I am using the following conditional statement to read from standard input.

if ((n = read(0,buf,sizeof(buf))) != 0)

When inputting data fro

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-10 21:15

    You better use fgets() for your task (catching user input), but even fgets() stores newline character in buffer.

    However if newline is there, you can be sure it is last character in the string so it is easy to strip it.

提交回复
热议问题