C - 将 .doc 文件读取为 32 位二进制数

C - Read .doc file as 32 bit binary numbers

这只是一个练习任务,因为 "Every file is just a binary file with different set of bytes.",我正在读取一个 .doc 文件作为二进制文件,假设

"this .doc file is actually a binary file with 32-bit unsigned numbers and i want to sort them as ascending within the file."

到目前为止,我的逻辑是计算文件中的总字节数除以 sizeof(int) ,然后生成该大小的 int 数组。并开始读取字节。但是这个逻辑有问题: file.doc 大小为 250 bytes 其中 sizeof(int) = 4,现在 250/4 = 62.5,我制作了一个包含 63 个整数的数组,现在我将如何处理 63rd integer ,作为 32 位二进制数 ?

执行类似 rem = filesize % sizeof(int) 的操作以获取 sizeof(int) 字节的最后一个连续块之后的字节数,并为数组中的最后一个整数执行 fread(&array[i], rem, 1, stream).

通常这些问题是通过添加填充来解决的。您可以假装原始文件已经用零填充(例如),直到下一个 4 字节的倍数。