[英]Read binary file to integer range of -32767 to 32767
I need to make a program to read a binary file into the range of -32767 to 32767. So far, the script below read the binary file into the range of -128 to 127.我需要编写一个程序,将二进制文件读入-32767到32767的范围内,到目前为止,下面的脚本将二进制文件读入了-128到127的范围。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp = NULL;
signed char shint[2000] = "";
int i = 0;
size_t bytes = 0;
if ((fp = fopen("raw_data.ht3", "rb")) == NULL) {
printf ("could not open file\n");
return 0;
}
if ((bytes = fread(&shint, 1, 2000, fp)) > 0 ) { //bytes more than 0
for (i = 0; i < bytes; i++) {
printf ("%d\n", shint[i]);
}
}
fclose(fp);
return 0;
}
More info about the binary file, my lecturer said the binary file should be read into 4 bytes data (I'm not sure my wording is right here).关于二进制文件的更多信息,我的讲师说应该将二进制文件读入 4 字节数据(我不确定我的措辞是否正确)。 The data is very big so I stop the data reading till 2000 data.数据非常大,所以我停止读取数据,直到 2000 个数据。 Though in the future I need to read all of them.虽然将来我需要阅读所有这些。
The final data representation最终数据表示
This is how I want to plot at the end of the day.这就是我想在一天结束时绘制的方式。 I will call our matlab or scilab after getting the desired data.得到想要的数据后,我会调用我们的 matlab 或 scilab。
Thanks!谢谢!
Use 4
bytes representation for your input data, ie replace对输入数据使用4
字节表示,即替换
signed char shint[2000] = "";
with和
long int shint[2000] = "";
and和
if ((bytes = fread(&shint, 1, 2000, fp)) > 0 ) { //bytes more than 0
with和
if ((bytes = fread(&shint, 4, 2000, fp)) > 0 ) { //bytes more than 0
and和
printf ("%d\n", shint[i]);
with和
printf ("%ld\n", shint[i]);
Note:笔记:
By the name of your variable ( shint
, ie short int
) and by the range -32768
to +32767
it seems that your instructor wanted 2
bytes for numbers, not 4
.根据您的变量名称( shint
,即short int
)以及-32768
到+32767
的范围,您的讲师似乎想要2
个字节的数字,而不是4
。
In that case use short int
(or simply short
) in your declaration and 2
as the second parameter of fread()
function.在这种情况下,在声明中使用short int
(或简称为short
)并将2
作为fread()
函数的第二个参数。
I don't have your data to test on (and I didn't tested out my answer) but it should be something like this:我没有你的数据来测试(我没有测试我的答案)但它应该是这样的:
First of all signed char shint[2000] = "";
首先signed char shint[2000] = "";
is holding 2000 signed chars (which are indeed signed 8 bit values have a look here - this is a very handy resource when handling data types sizes) , so you need some value to hold signed 32 bit (4 byte) values, which depends on your machine architecture, assuming it is 32 bit integer ( it is not difficult to find out ) you could hold you values in int shint[2000] = "";
持有 2000 个有符号字符(它们确实是有符号的 8 位值,请看这里- 这是处理数据类型大小时非常方便的资源),因此您需要一些值来保存有符号的 32 位(4 字节)值,这取决于您的机器架构,假设它是 32 位整数( 不难找出),您可以将值保存在int shint[2000] = "";
next thing you need to pay attention to is the function fread
here is some friendly documentation , the second parameter to this function (which is 1 in your code) should be the number of bytes represents a single value from the data you want to read from, so in your situation should be 4 (bytes).接下来你需要注意的是函数fread
这里是一些友好的文档,这个函数的第二个参数(在你的代码中是 1)应该是字节数,代表你想要读取的数据中的单个值,所以在你的情况下应该是 4(字节)。 the other parameters should be OK.其他参数应该没问题。
edit: to make sure you are reading 4 bytes you can use indeed the answer given by MarianD and store long
values.编辑:为了确保您正在阅读 4 个字节,您确实可以使用 MarianD 给出的答案并存储long
值。
As i understand you want easy access to chars and signed 16 bits ints.据我了解,您希望轻松访问字符和带符号的 16 位整数。
#define SIZE 2000
union
{
char shint_c[SIZE * 2];
short shint[SIZE];
}su;
and then in your if然后在你的 if
fread(&su, 2, SIZE, fp)
and in the loop to print shorts并在循环中打印短裤
printf ("%hd\n", su.shint[i]);
or 8 bits ints或 8 位整数
printf ("%hhd\n", su.shint_c[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.