I used fwrite to store some data and now I'm trying to use fread to read the data from the txt file for processing. I want to read the values individually but I can't figure out how you'd do that. This is what I have tried:
#include <stdio.h>
#include <stdlib.h>
int main () 
{
  FILE * pFile;
  long lSize;
  unsigned short * buffer;
  size_t result;
  pFile = fopen ( "myfile.txt" , "rb" );
  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);
  // allocate memory to contain the whole file:
  buffer = (unsigned short *) malloc (sizeof(unsigned short)*lSize);
  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  printf("%uz\n", result);
  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}
The above program compiles fine but when I run it with ./a.out I get a Segmentation fault. When I run it with sudo ./a.out I don't get seg fault but nothing prints out. 
Any idea what I can do to fix it?
 
     
    