使用函数 fgetpos()
和 fsetpos()
与使用函数 ftell()
和 fseek 有什么区别()
获取和设置文件中的位置?
fgetpos()
和 fsetpos()
有什么用?为什么要使用它们来代替 ftell()
和 fseek()
?
最佳答案
以上答案都不正确 - 事实上,如果您将 fsetpos
与 fseek
互换使用,您可能会引入安全漏洞 (https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=20087255) .
原因是 fsetpos
的 fpos_t *pos
参数实际上不是一个整数,因此它不能用于查找文件中的任意位置。因此,唯一有效的值是从 fgetpos
获得的值。正如文档所说,
The internal file position indicator associated with stream is set to the position represented by
pos
, which is a pointer to anfpos_t
object whose value shall have been previously obtained by a call tofgetpos
.
( http://www.cplusplus.com/reference/cstdio/fsetpos/ )
如果你想要的只是能够搜索到超过 32 位边界的任意位置,那么使用 ftello
/fseeko
并使用 #define _FILE_OFFSET_BITS 64 进行编译
。
关于c - fgetpos/fsetpos 和 ftell/fseek 之间有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3348637/