Permalink
Browse files

Function Reads Binary File and prints out its contents

  • Loading branch information...
asadpiz committed Jun 18, 2016
1 parent 019a5f7 commit f064ad3bdb37bbfd0d9f982c73891bbe47724d8d
Showing with 32 additions and 9 deletions.
  1. +32 −9 viewer.c
View
@@ -1,14 +1,37 @@
#include <stdio.h>
int main (int argc, char* argv[]){
unsigned char buffer[1];
FILE *in_file = fopen(argv[1], "rb"); // read only
int i;
printf ("Filename is: %s\n", argv[1]);
fread(buffer, sizeof(unsigned char), 1, in_file);
fclose(in_file);
//for(i = 0; i < 40; i++)
// { printf("buffer[%d] == %d\n", i, buffer[i]);}
FILE* geometry_file = NULL;
geometry_file = fopen(argv[1], "rb"); // Open .g File
if (geometry_file == NULL){
printf ("ERROR Opening File\n");
return 1;
}
else{
int i;
size_t size; /*Size of File*/
unsigned char *buffer; /*Variable/buffer file is stored in*/
fseek(geometry_file, 0L, SEEK_END);
size = ftell(geometry_file); /*calc the size needed*/
rewind (geometry_file);
buffer = (unsigned char *) malloc(size);
if (buffer == NULL){
printf ("ERROR Allocating Buffer\n");
return 1;
}
else{
fread(buffer, sizeof(unsigned char), size, geometry_file);
// Print Entire file
for (i=0; i<size; i++){
printf("%02x", buffer[i]);
}
}
fclose (geometry_file);
free(buffer);
return 0;
}
}

0 comments on commit f064ad3

Please sign in to comment.