如何保存uint64_t中字节在C文件? [英] How to save uint64_t bytes to file on C?

查看:264
本文介绍了如何保存uint64_t中字节在C文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存uint64_t中咬在普通的C文件?

How to save uint64_t bites to file on plain C?

正如我想象输出文件将在8个字节的长度?

As i suppose output file will be 8 bytes length?

推荐答案

修改由于我的编译器不具有 uint64_t中我已经证明了两如何拯救一个64位的值到文件,使用无符号长长。第一个例子中(十六进制)的文本格式,所述第二二进制其写入。

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.

注意无符号长长可能会在某些系统上超过64位。

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

myfile.txt的内容(十六进制转储)

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

myfile.bin(十六进制转储)的含量(小端)

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

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

这篇关于如何保存uint64_t中字节在C文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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