Read from pipe line by line in C

后端 未结 2 1322
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 04:32

How can I separate the lines which are coming from a pipe. In the pipe there is this text:

HALLO:500\\n
TEST:300\\N
ADAD
ADAWFFA
AFAGAGAEG

I wa

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-06 04:47

    Here's another option (I am not totally sure it is a 'proper' way)- use the number of bytes read by the read function. In this example I was reading from stdin although a redirection was made so the fd in 0 is a file/pipe/whatever you need it to be.

      while ((nbytes=read(STDIN_FILENO, buffer, MAX_PIPE_SIZE)) > 0) {
        write(STDOUT_FILENO, buffer, nbytes);
      }
    

提交回复
热议问题