这里我只想查看变量pos
的内容;那么必须为此使用什么类型的格式说明符?
#include <stdio.h>
void main() {
FILE *fileptr = fopen("sample.txt", "r+");
fpos_t pos;
int val = fgetpos(fileptr, &pos);
}
最佳答案
fpos_t
在 C 标准中定义为:
fpos_t
which is a complete object type other than an array type capable of recording all the information needed to specify uniquely every position within a file.
此类型不一定是标量,它可以定义为具有多个成员的 struct
。没有 printf
格式可以以可读的方式输出它。某些系统可能需要执行复杂的任务来处理文本文件,并非每台计算机都是 PC。
如果您对实现感兴趣,您可以将其内容转储为十六进制字节:
#include <stdio.h>
int main(void) {
FILE *fileptr = fopen("sample.txt", "r+");
fpos_t pos;
if (fileptr && fgetpos(fileptr, &pos) == 0) {
printf("contents of pos: ");
for (size_t i = 0; i < sizeof(pos); i++) {
printf("%02X", ((unsigned char *)&pos)[i]);
}
printf("\n");
}
return 0;
}
在许多现代系统上,您将得到 0000000000000000
,因为 64 位整数足以表示文件偏移量所需的所有信息,但您不应该依赖它。
关于c - fpos_t 类型变量的格式说明符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39587916/