c - fgetpos/fsetpos 和 ftell/fseek 之间有什么区别

标签 c c-standard-library

使用函数 fgetpos()fsetpos() 与使用函数 ftell()fseek 有什么区别() 获取和设置文件中的位置?

fgetpos()fsetpos() 有什么用?为什么要使用它们来代替 ftell()fseek()

最佳答案

以上答案都不正确 - 事实上,如果您将 fsetposfseek 互换使用,您可能会引入安全漏洞 (https://www.securecoding.cert.org/confluence/pages/viewpage.action?pageId=20087255) .

原因是 fsetposfpos_t *pos 参数实际上不是一个整数,因此它不能用于查找文件中的任意位置。因此,唯一有效的值是从 fgetpos 获得的值。正如文档所说,

The internal file position indicator associated with stream is set to the position represented by pos, which is a pointer to an fpos_t object whose value shall have been previously obtained by a call to fgetpos.

( 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/

相关文章:

c - 如何在所有前台进程或特定前台进程之前运行后台进程?

c - 为什么没有“unsigned wchar_t”和“signed wchar_t”类型?

c - 使用 write 在文件中写入 int 变量

关于操作数求值顺序的矛盾

c - getch() 和 getchar() 有什么区别?

c - 一起编写时如何解析/评估两个单独的运算符?

c++ - 无法通过套接字发送或接收数据

c++ - 为什么没有int128_t?

c - TFTP 客户端(基于 ENET)无法连接到远程 TFTP 服务器

c - 存储整数和算术运算符的数据类型?