Stop on newline when using read(…)

前端 未结 2 1085
傲寒
傲寒 2021-01-26 15:00

I need to read the NMEA sentences from a GPS connected through UART. The OS is Debian, and the language must be C++. To do so I\'m opening the file with open(...) a

2条回答
  •  情书的邮戳
    2021-01-26 15:45

    How may I use read(...) and stop on new line? Is there an option to read(...)?

    No, read() has no option to do that.

    Per the POSIX standard:

    The read() function reads data previously written to a file. If any portion of a regular file prior to the end-of-file has not been written, read() shall return bytes with value 0. For example, lseek() allows the file offset to be set beyond the end of existing data in the file. If data is later written at this point, subsequent reads in the gap between the previous end of data and the newly written data shall return bytes with value 0 until data is written into the gap.

    Upon successful completion, where nbyte is greater than 0, read() shall mark for update the last data access timestamp of the file, and shall return the number of bytes read. This number shall never be greater than nbyte. The value returned may be less than nbyte if the number of bytes left in the file is less than nbyte, if the read() request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer than nbyte bytes immediately available for reading. For example, a read() from a file associated with a terminal may return one typed line of data.

    If a read() is interrupted by a signal before it reads any data, it shall return -1 with errno set to [EINTR].

    If a read() is interrupted by a signal after it has successfully read some data, it shall return the number of bytes read.

    ...

    read() handles raw bytes, without any interpretation.

    If you want to use a library function to read lines of text data from a file, you can use the getline() function.

提交回复
热议问题