fpos_t类型变量的格式说明符? [英] Format Specifier for fpos_t type variable?

查看:101
本文介绍了fpos_t类型变量的格式说明符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我只想查看变量pos的内容;那么必须使用哪种类型的格式说明符?

Here I just want to see the content of variable pos ; so what type of format specifier have to used for that ?

#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 .

This type is not necessarily scalar, it could be defined as a struct with multiple members. There is no printf format to output it in a readable fashion. Some systems may need to perform complex tasks to handle text files, not every computer is a PC.

如果您对实现感兴趣,可以将其内容转储为十六进制字节:

If you are interested in the implementation, you could dump its contents as hex bytes:

#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位整数足以表示表示文件中的偏移量所需的所有信息,但您不应依赖于此.

On many modern systems, you will get 0000000000000000 because a 64 bit integer is sufficient for all information needed to represent the offset into the file, but you should not rely on this.

这篇关于fpos_t类型变量的格式说明符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆