从C中的文件中读取数字
我主要是一名 Python 程序员,但我一直在使用 C,因为 Python 对于图形来说太慢了(20 fps 移动分形 FTW)。但我遇到了症结... 我在十六进制编辑器中编写了一个小文件来进行测试。当我尝试读取第一个字节 5A 时,它使用这样的程序正确地给出了 90...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
data=fopen("C:\\vb\\svotest1.vipc","r+b");
unsigned char number;
fread(&number,1,1,data);
printf("%d\n",number);
}
但是当我尝试将前四个字节 5A F3 5B 20 读取为整数时,我得到 542896986
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
data=fopen("C:\\vb\\svotest1.vipc","r+b");
unsigned long number;
fread(&number,1,4,data);
printf("%d\n",number);
}
它应该是 1525898016! ! 问题是它颠倒了字节顺序。嘎啊!当然!该程序的工作方式取决于机器。现在我们开始讨论这个问题了,即使是字节也不能在每台机器上工作!
所以我需要帮助......在Python中,我可以使用struct.pack和struct.unpack使用任何格式(长,短,单,双,有符号,无符号,大端,小端)将数据打包成字节并解压它。我需要在 C 中编写这样的东西...我可以自己编写,但我不知道如何编写。
I am primarily a Python programmer, but I've been working with C because Python is too slow for graphics (20 fps moving fractals FTW). I'm hitting a sticking point though...
I wrote a little file in a hex editor to test with. When I try reading the first byte, 5A, it correctly gives me 90 with a program like this...
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
data=fopen("C:\\vb\\svotest1.vipc","r+b");
unsigned char number;
fread(&number,1,1,data);
printf("%d\n",number);
}
But when I try reading the first four bytes, 5A F3 5B 20, into an integer I get 542896986
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
FILE *data;
int main(int argc, char* argv[])
{
data=fopen("C:\\vb\\svotest1.vipc","r+b");
unsigned long number;
fread(&number,1,4,data);
printf("%d\n",number);
}
It should be 1525898016!!!
The problem is it has reversed the byte order. GAH! Of course! The way this program works will depend on the machine. And now that we are on the subject, even the byte won't work on every machine!
So I need help... In Python I can use struct.pack and struct.unpack to pack data into bytes using any format (long, short, single, double, signed, unsigned, big endian, little endian) and unpack it. I need something like this in C... I could write it myself but I don't know how.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可移植地处理此问题的最简单方法可能是在将数据写入文件之前对数据使用
htonl
(因此文件将采用网络/大端顺序)和ntohl
当您阅读时(将网络订单数据转换为本地约定,无论是什么)。如果您必须执行的操作不仅仅是一种类型的几个值,您可能需要为此目的查看一个更完整的库,例如 Sun XDR 或 Google 协议缓冲区。
The easiest way to handle this portably is probably to use
htonl
on the data before you write it to the file (so the file will be in network/big endian order) andntohl
when you read (converts the network order data to the local convention, whatever that is).If you have to do much more than a few values of one type, you may want to look into a more complete library for the purpose, such as Sun XDR or Google Protocol Buffers.
我认为你可以弄清楚(字节序)http://en.wikipedia.org/wiki/Endianness 然后读取字节顺序。
I think you can figure out the (Endianness) http://en.wikipedia.org/wiki/Endianness and then read the byte order.
像这样?
这导致第一部分是最重要的。我想这就是你想要的。
Like this?
This results in the first part being the most significant. I think that's what you want.