Read in binary file ( pointers )

This is a discussion on Read in binary file ( pointers ) within the C Programming forums, part of the General Programming Boards category; Hi, having some problems reading in a binary file, using the fread() command. The function below is meant to take ...

  1. #1
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33

    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;
    };

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Doin this on Windows? If so you MUST open the file in binary mode!

    You should realize that it is unsafe to use the width*height values supplied in the file directly, always do some sanity checking on them before allocating memory.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    okay, obvious question time :S

    how do i open the file in binary mode and what does fopen() do ?

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    fopen(fname,"rb");

    What do you mean what does fopen do? It opens the file for input or output (or both).

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    sorry got confused about fopen and if there was a different binary variation, but got it now.

    Anyway I am still getting the wrong data back, I have even gone as far as found out the total count (as fread() is meant to return total number of items read) but when the value of count is printed out it returns (EDIT)the correct amount (which for a ppm image of 492 by 708 is 1045008 [3*width*height])(EDIT)

    any other ideas :/ about what could be affecting the fread line/or function as a whole?

    thanks for the help,
    Giant.

    p.s. I used fopen(fname, "rb") and this passed the if defination on the fread line, but like i said above still getting wrong values back :_(
    Last edited by Giant; 06-21-2005 at 09:38 AM.

  6. #6
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Post your updated code and I will take a look. If you have some sample input, you can attatch that as well.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  7. #7
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    read up abit so apparently the count of fread thinks it is taking in the full amount....

  8. #8
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    i have just worked out what it is doing,

    it is reading in the correct hex values but as it reads more it it pushes them along the buffer so my output looks like -: 6a293870, the hex file actually goes (after header) 70 38 29 6a

    when infact i need the output to be 70 (and thats it) any ideas why i am going wrong on that front ?

  9. #9
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    i have just checked and it seems to be reading the file in, in 4 byte chunks per array

    EDIT:

    Code:
    if((count = (fread(ptr_data, 1, 3*NumByteRead, fptr))) != 3*NumByteRead)
    returns the same 4 byte chunk as ->

    Code:
    if((count = (fread(ptr_data, 100, 3*NumByteRead, fptr))) != 3*NumByteRead)
    Last edited by Giant; 06-21-2005 at 10:08 AM.

  10. #10
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I think you need to post some code. Strip the problematic code out to a separate program and make it as small as you can that still replicates the problem. Be sure to include the appropriate data file!

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  11. #11
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    Well that didnt take long to shorten as i thought it would ....

    anyway here is the code, still giving me the same error

    Code:
    #include "practice.h"
    /*
    	Expect a series of images of the form base0000.ppm, base0001.ppm, etc
    	Supports forward or reverse image ordering
    */
    void ReadImageData(char *, headerstruct*);
    int main(int argc, char **argv)
    { 
    	filenamestruct *ptr_file;
    	headerstruct *ptr_header;
    	ptr_file = new filenamestruct;
    	ptr_header = new headerstruct;
    	
    	strncpy(ptr_file->nextFileName,argv[1], strlen(ptr_file->nextFileName));
    	strncpy(ptr_header->height,"708",3);
    	strncpy(ptr_header->width,"492",3);
    	ptr_header->headerend = 15;
    
    	ReadImageData(ptr_file->nextFileName, ptr_header);
    }
    
    void ReadImageData(char * fname, headerstruct *temp_head){
    	
    	int *ptr_data;
    	int i=0, NumByteRead = 0;
    	FILE *temp_fptr;
    	size_t j=0, count = 0, sizeofoneitem = 1;
    
    	NumByteRead = (atoi(temp_head->width) * atoi(temp_head->height));
    	ptr_data = new int[3*NumByteRead];
    
    	temp_fptr = fopen(fname,"rb");
    	while(j<(temp_head->headerend)){j++;getc(temp_fptr);}
    
    	fread(ptr_data, sizeofoneitem, (3*NumByteRead), temp_fptr);
    	printf("\nValue in first buffer(ptr_data[0]) - %x", ptr_data[0]); //max of 261251
    	fclose(temp_fptr);
    }

  12. #12
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    Code:
    struct filenamestruct{
    	char basename[128]; 
    	char basefolder[128]; 
    	char basenumber[128];
    	char nextFileName[256];
    	size_t maximagenumber;
    	size_t originalbasenumber;
    };
    
    struct headerstruct{
    	char fullheader[32];
            char prefix[4];
    	char width[4];
    	char height[4];
    	char pixelmaxvalue[4];
    	size_t headerend;
    };
    sorry forgot structures

  13. #13
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    What is in practice.h?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  14. #14
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    Nothing much just the includes (you can see them below as i hav streamlined some more to make it as simple as possible, though i still cant see where i am going wrong )

    Code:
    #include <iostream>
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    #include "ctype.h"
    using namespace std;/*
    	Expect a series of images of the form base0000.ppm, base0001.ppm, etc
    	Supports forward or reverse image ordering
    */
    int main(int argc, char **argv)
    { 
    	int *ptr_data;
    	int i=0, NumByteRead = 0, height, width;
    	FILE *temp_fptr;//, *test;
    	size_t j=0, count = 0, sizeofoneitem = 1,headerend=0;
    
    	height=708;
    	width=492;
    	headerend = 15;
    	
    	NumByteRead = 3*width*height;
    
    	ptr_data = new int[NumByteRead];
    	temp_fptr = fopen(argv[1],"rb");
    
    	while(j<headerend){
    		j++;
    		//fprintf(test,"%2x",
    		getc(temp_fptr);//);
    	} //jumps through header file
    
    	fread(ptr_data, sizeofoneitem, (NumByteRead), temp_fptr);
    	printf("\nValue in first buffer(ptr_data[0]) - %02x", ptr_data[0]); //max of 261251
    	
    	fclose(temp_fptr);
    }

  15. #15
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Is this what you are working on: http://netpbm.sourceforge.net/doc/ppm.html? If so, why reinvent the wheel?

    In order to debug your code I will need the exact ppm file you are using (it sounds like you can zip it up and get it very small).

    BTW, this is the exact code I will be working with (I cleaned it up and formatted it):

    Code:
    #include <cstdio>
    
    using namespace std;
    /*
        Expect a series of images of the form base0000.ppm, base0001.ppm, etc
        Supports forward or reverse image ordering
    */
    int main(int argc, char **argv) {
        int *ptr_data;
        int i=0, NumByteRead = 0, height, width;
        FILE *temp_fptr;
        size_t j=0, count = 0, sizeofoneitem = 1,headerend=0;
    
        height=708;
        width=492;
        headerend = 15;
    
        NumByteRead = 3*width*height;
    
        ptr_data = new int[NumByteRead];
        temp_fptr = fopen(argv[1],"rb");
    
        while(j<headerend) {
            j++;
            getc(temp_fptr);
        }
    
        fread(ptr_data, sizeofoneitem, (NumByteRead), temp_fptr);
        printf("\nValue in first buffer(ptr_data[0]) - %02x", ptr_data[0]);
        fclose(temp_fptr);
        return 0;
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read a file (binary) into an array of structs
    By Separ in forum C Programming
    Replies: 3
    Last Post: 04-14-2009, 09:09 PM
  2. Binary file is read with bytes missing
    By thomas41546 in forum C# Programming
    Replies: 1
    Last Post: 09-03-2006, 04:15 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21