-1

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.

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.

Thanks!

adiga
  • 34,372
  • 9
  • 61
  • 83
fizsics
  • 1
  • 3
  • You probably want to read either 2 or 4 bytes at a time. (Your question title suggests two bytes, your lecturer said four). You can probably use `fread`. (Byte swapping is theoretically an issue, but for this exercise you can probably ignore it.) – Steve Summit Oct 08 '17 at 14:40
  • How do I read 2 or 4 bytes at a time? – fizsics Oct 08 '17 at 14:41
  • Your question says that you want a short[] array. Your teacher said you want an int[] array. – Hans Passant Oct 08 '17 at 15:07
  • @HansPassant I think I need to stick with 4 bytes per reading. – fizsics Oct 08 '17 at 15:15
  • To avoid differences in type size between compilers, if the specification is that the integers in the file are 4 bytes each, it would be safest to include `` and use either `int32_t` or `unint32_t`, depending on whether the integers are supposed to be interpreted as signed or unsigned. – Steve Summit Oct 08 '17 at 16:06
  • Please do not vandalize your posts which invalidate the comments and answers posted below. By posting on the Stack Exchange network, you've granted a non-revocable right for SE to distribute that content (under the [CC BY-SA 3.0 license](https://creativecommons.org/licenses/by-sa/3.0/)). By SE policy, any vandalism will be reverted. If you would like to disassociate this post from your account, see [What is the proper route for a disassociation request?](https://meta.stackoverflow.com/q/323395) – adiga Oct 24 '17 at 07:42

3 Answers3

0

Use 4 bytes representation for your input data, i. e. 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, i. e. 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.

MarianD
  • 13,096
  • 12
  • 42
  • 54
0

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.

Dima Gimburg
  • 1,376
  • 3
  • 17
  • 35
0

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]);
0___________
  • 60,014
  • 4
  • 34
  • 74