Pravin Kamble
Pravin Kamble

Reputation: 81

Format Specifier for fpos_t type variable?

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);
}

Upvotes: 3

Views: 4306

Answers (2)

chqrlie
chqrlie

Reputation: 145287

fpos_t is defined in the C Standard as :

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.

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;
}

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.

Upvotes: 5

Bathsheba
Bathsheba

Reputation: 234865

fpos_t is intentionally an opaque structure.

Furthermore, the C standard doesn't define a way of inspecting its contents (there's no toString() nonsense that you see in other languages like Java).

So if you want a portable solution then you're out of luck.

If it's for debugging on a particular platform, then you might be able to figure out its contents (quite often it contains an integral type specifying little more than a byte count), and output appropriately.

Upvotes: 4

Related Questions