diff --git a/libraries/AP_Filesystem/AP_Filesystem.cpp b/libraries/AP_Filesystem/AP_Filesystem.cpp index c780c5a9b1663..09bfcd05a7ad4 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem.cpp @@ -321,19 +321,31 @@ bool AP_Filesystem::fgets(char *buf, uint8_t buflen, int fd) { const Backend &backend = backend_by_fd(fd); + // we will need to seek back to the right location at the end + auto offset_start = backend.fs.lseek(fd, 0, SEEK_CUR); + if (offset_start < 0) { + return false; + } + + auto n = backend.fs.read(fd, buf, buflen); + if (n <= 0) { + return false; + } + uint8_t i = 0; - for (; i