[Solved] How to save uint64_t bytes to file on C?


EDIT. Since my compiler does not have uint64_t I have shown two ways to save a 64-bit value to file, by using unsigned long long. The first example writes it in (hex) text format, the second in binary.

Note that unsigned long long may be more than 64 bits on some systems.

#include <stdio.h>

int main(void) {

    FILE *fout;
    unsigned long long my64 = 0x1234567887654321;

    // store as text
    fout = fopen("myfile.txt", "wt");
    fprintf(fout, "%llX\n", my64);
    fclose(fout);    

    // store as bytes
    fout = fopen("myfile.bin", "wb");
    fwrite(&my64, sizeof(my64), 1, fout);
    fclose(fout);    

    return 0;
}

Content of myfile.txt (hex dump)

31 32 33 34  35 36 37 38  38 37 36 35  34 33 32 31  1234567887654321
0D 0A                                               ..

Content of myfile.bin (hex dump) (little-endian)

21 43 65 87  78 56 34 12                            !Ce‡xV4.

9

solved How to save uint64_t bytes to file on C?