如何确保将4个字节写入二进制文件 [英] How to ensure writing 4 bytes to binary file

查看:145
本文介绍了如何确保将4个字节写入二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对一个16/32位的EEPROM进行编程.我正在从C程序编写文件,但是 fwrite()似乎仅执行8位操作?我写了一个简单的示例,并使用xxd(和hexdump)查看结果,但是文件似乎只有8位.由于hexdump&的限制,我是否只看到8位?xxd,还是问题出在 fwrite()?

I want to program an EEPROM which is 16/32 bit. I am writing the file from a C program, but fwrite() seems to be doing only 8 bits? I wrote a simple example, and using xxd (and hexdump) to look at the results, but the file seems to be only 8 bits. I wonder if I'm only seeing 8 bits due to the limitations of hexdump & xxd, or if the problem is with fwrite()?

任何人都知道如何检查所有位是否都已写入文件吗?

Anyone know how I can check that all the bits are being written to the file?

#include <stdio.h>
#include <stdlib.h>

const unsigned int dataSize = 255;
unsigned long tmp[] = { 0xFFFF, 0xFFDD, 0xFDDD, 0xF000, 0x0F0F, 0x0001, 0x1010 };
  
int main() {
    const char *path = "text.bin";
    FILE *fp = fopen(path, "wb"); 
    const void *data = tmp;
 
    if (!fp) {
        fprintf(stderr, "fopen() failed for '%s'\n", path);
    } else {
        fwrite(data, 1, dataSize, fp);
    }
  
    return 0;
}

使用 xxd ,我看到了:


00000000: ffff 0000 0000 0000 ddff 0000 0000 0000  ................
00000010: ddfd 0000 0000 0000 00f0 0000 0000 0000  ................
00000020: 0f0f 0000 0000 0000 0100 0000 0000 0000  ................
00000030: 1010 0000 0000 0000 0000 0000 0000 0000  ................
00000040: c005 5182 b27f 0000 0000 0000 0000 0000  ..Q.............
00000050: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000060: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000070: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000080: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00000090: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000a0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000b0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000e0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
000000f0: 0000 0000 0000 0000 0000 0000 0000 00    ...............

编辑:我试图确保有32位写入文件. xxd 的结果是:

EDIT: I'm trying to ensure there are 32 bits written to the file. The results of xxd are:


00000000: 11111111 11111111 00000000 00000000 00000000 00000000  ......

哪个显示 0xFFFF 有16位,但是其他16位在哪里?理想情况下,我希望看到32个边界的 0xFFFF 的16个零,然后是16个零.

Which show there are 16 bits for the 0xFFFF but where are the other 16 bits? Ideally I'd like to see 16 zeros followed by 16 ones for 0xFFFF for a 32 bit boundary.

我想知道这是不是 xxd 软件没有显示它而不是C没有编写它的问题.

I'm wondering if this is a problem with the xxd software not displaying it, rather than C not writing it.

(我意识到名称和文件指针没有关闭,这只是我在2分钟内为了帮助显示问题而将其敲掉的东西,我是从空中挑出255个的.)

(I realise the names and the file pointer isn't closed, it was just something I knocked up in 2 minutes to help display the problem, I picked 255 out of the air.)

推荐答案

要控制文件中的确切输出,应使用< stdint.h> 中的确切宽度类型,而不是 unsigned long ,在您的系统上似乎有64位.

To control the exact output in the file, you should use the exact width types from <stdint.h> instead of unsigned long, which seems to have 64 bits on your system.

之所以看不到16个0后面紧跟16个1,是因为目标系统使用Little-endian表示大于8位的整数类型. Endianness 确定这些类型在内存中的字节顺序.

The reason you do not see 16 zeroes followed by 16 ones is your target system uses little-endian representation for integer types larger than 8 bits. Endianness determines the order of bytes in memory for these types.

此外,您不应从大小为28个字节的 tmp 数组中写入255个字节.

Furthermore, you should not write 255 bytes from the tmp array which has a size of 28 bytes.

这是修改后的版本:

#include <stdio.h>
#include <stdint.h>

// data has 256 bytes
uint32_t data[64] = { 0xFFFF, 0xFFDD, 0xFDDD, 0xF000, 0x0F0F, 0x0001, 0x1010 };
  
int main() {
    const char *path = "text.bin";
    FILE *fp = fopen(path, "wb"); 
 
    if (!fp) {
        fprintf(stderr, "fopen() failed for '%s'\n", path);
    } else {
        size_t written = fwrite(data, 1, sizeof data, fp);
        if (written != sizeof data) {
            fprintf(stderr, "fwrite() only wrote %zu bytes\n", written);
        }
        fclose(fp);
    }
    return 0;
}

这篇关于如何确保将4个字节写入二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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