diff --git a/src/platform.cpp b/src/platform.cpp index 2f04ee0068..866e34b4cc 100644 --- a/src/platform.cpp +++ b/src/platform.cpp @@ -767,9 +767,15 @@ bigint platform::ftell(FILE *fp) int platform::fseek(FILE *fp, bigint pos) { #if defined(_WIN32) - return ::_fseeki64(fp, (__int64) pos, SEEK_SET); + if (pos == platform::END_OF_FILE) + return ::_fseeki64(fp, 0, SEEK_END); + else + return ::_fseeki64(fp, (__int64) pos, SEEK_SET); #else - return ::fseek(fp, (long) pos, SEEK_SET); + if (pos == platform::END_OF_FILE) + return ::fseek(fp, 0, SEEK_END); + else + return ::fseek(fp, (long) pos, SEEK_SET); #endif } diff --git a/src/platform.h b/src/platform.h index b7c5d28561..97057006ad 100644 --- a/src/platform.h +++ b/src/platform.h @@ -263,7 +263,12 @@ namespace platform { bigint ftell(FILE *fp); + /*! constant to seek to the end of the file */ + constexpr bigint END_OF_FILE = -1; + /*! Set absolute file position + * + * If the absolute position is END_OF_FILE, then position at the end of the file. * * \param fp FILE pointer of the given file * \param pos new position of the FILE pointer diff --git a/unittest/utils/test_platform.cpp b/unittest/utils/test_platform.cpp index 6c17b8876e..c5d04d3b94 100644 --- a/unittest/utils/test_platform.cpp +++ b/unittest/utils/test_platform.cpp @@ -170,7 +170,9 @@ TEST(Platform, fseek_ftell) ASSERT_EQ(platform::fseek(fp, 15), 0); ASSERT_EQ(fgetc(fp), '6'); fflush(fp); - fseek(fp, -1, SEEK_END); + ASSERT_EQ(platform::fseek(fp, platform::END_OF_FILE), 0); + ASSERT_EQ(platform::ftell(fp), 21); + ASSERT_EQ(platform::fseek(fp, 20), 0); ASSERT_EQ(fgetc(fp), 0); ASSERT_EQ(platform::ftell(fp), 21); fclose(fp);