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.
#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). The data is very big so I stop the data reading till 2000 data. Though in the future I need to read all of them.
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.
Thanks!
Use 4
bytes representation for your input data, ie replace
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
.
In that case use short int
(or simply short
) in your declaration and 2
as the second parameter of fread()
function.
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] = "";
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] = "";
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). 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.
As i understand you want easy access to chars and signed 16 bits ints.
#define SIZE 2000
union
{
char shint_c[SIZE * 2];
short shint[SIZE];
}su;
and then in your if
fread(&su, 2, SIZE, fp)
and in the loop to print shorts
printf ("%hd\n", su.shint[i]);
or 8 bits ints
printf ("%hhd\n", su.shint_c[i]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.