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
How may I use
read(...)and stop on new line? Is there an option toread(...)?
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
nbyteis 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 thannbyte. The value returned may be less thannbyteif the number of bytes left in the file is less thannbyte, if theread()request was interrupted by a signal, or if the file is a pipe or FIFO or special file and has fewer thannbytebytes immediately available for reading. For example, aread()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 witherrnoset 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.