Read in binary file ( pointers )
Hi, having some problems reading in a binary file, using the fread() command.
The function below is meant to take in a filename and header structure (structure not important) and open the file, which is a .ppm file if you are interested, then skip through the header of the file (which i believe it does) then fread all of the rest of the data in the file into one big array (width * height of ppm image). After this return the pointer (to the array where it has stored all of this data) to the function that called it....only problem is, that when i try to print out the buffer (variable - ptr_data in my code) i dont get what is in the file, and the fread only reads one item then terminates...
any help would be appreciated
Code:
int *ReadImageData(char * fname, headerstruct *temp_head){
int *ptr_data;//, *ptr_data_G, *ptr_data_B;
int i=0, NumByteRead = 0, count = 0;
FILE *fptr;
size_t j=0;
NumByteRead = (atoi(temp_head->width) * atoi(temp_head->height));
ptr_data = new int[NumByteRead];
if((fptr = fopen(fname,"r")) == NULL) {
printf("\nFailed to Open file -: %s\n", fname);
exit(-1);
}
else{
while(j<(temp_head->headerend)){j++;getc(fptr);}
//printf("\nSkipping through header %d",j);
if(count = (fread(ptr_data, 1, NumByteRead, fptr)) != NumByteRead)
{
printf("\nTo few charaters read at fread %d - actual count %d\n", NumByteRead, count);
printf("\nValue in buffer - %x", ptr_data);
exit(-1);
}
}
fclose(fptr);
return (ptr_data);//, ptr_data_G, ptr_data_B);
}
just incase it is important
Code:
struct headerstruct{
char fullheader[32];
char prefix[4];
char width[4];
char height[4];
char pixelmaxvalue[4];
size_t headerend;
};