c - fpos_t 类型变量的格式说明符?

标签 c file

这里我只想查看变量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/

相关文章:

c - S_ISDIR 返回目录的不同值

java - 为什么不推荐使用 java.io.File?

c++ - QTextStream 无法使用 openmode QIODevice::Append 写入包含 "\t"的文件

java - 使用java读取 "ParseFile"

c - c程序中的char**是什么意思,谁能给个正确的解释

c++ - 免费(指针)方法的性能?

c - 我的代码无法理解空格 (' ' )并将完成代码

mysql - 错误 : ENOENT: no such file or directory, 打开 'D :\nodejs\loginnew\views\layouts\layout. Handlebars 这个错误是我发现的

c - 没有'\n'的printf()在libev中不起作用

c++ - 为什么 OpenCV 拒绝 cvLoadImage ("string.ext"),但接受 cvLoadImage(argv[1])?