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; I havent seen that webapge before tbh, i am on student placement right now from university, and my boss wants ...

  1. #16
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    I havent seen that webapge before tbh, i am on student placement right now from university, and my boss wants me to do things with .ppm files, but before i can manipulate them I need to be able to read them into a suitable array.

    how do you want me to send you the .ppm file (its 740 kb, which isnt a problem for me to send to you) ?

  2. #17
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Zip it up and post it as an attatchment. If you click on 'post reply' on the left-hand side of the screen (as opposed to using the 'Quick Reply' box) you have many more options. I am leaving in about a half hour, but I will try to get something for you either today or tomorrow if no one else helps out.

    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. #18
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    it wont let me upload it as it isnt a recognised filetype, and when i change the name it limits everything to 193.0 kb and this is 760 :S
    Last edited by Giant; 06-21-2005 at 01:25 PM.

  4. #19
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Try to zip it up and then rename it to .txt and see what happens.

    If you have a web server somewhere you can put it on and give me a link we can do it that way.

    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. #20
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    i already tried the zip (the file is originally 1Mb) i dont have hosting but asking a mate now, could i not email it to you ?

  6. #21
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I don't care for large files emailed to me. I probably won't be home after work for a couple of hours, but I can try to work on it this evening if you have worked out a place to put it where I can reach it. Do you have a smaller ppm file? I am quite certain you have made some fatal assumptions about the file format and are reading in junk as a consequence, but unless I see exactly what you are doing (i.e., can run a debugger with your datafile), I can't be certain it isn't just a simple little mistake. There are dozens (probably hundreds) of image file format each with probably at least a half-dozen different variations. The only way you can be positive your program will always handle the data correctly is to either get a third-party library (like at the link I suggested) or only read files generated by the same program. Giving a quick look at the ppm file format, it looks like there is a huge amount of variability in the whitespace layout, simply getting this into a block of memory is not likely to solve your long-term problem.

    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. #22
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    my good friend and soon to be flatmate has come through for me once again and the file is hosted (it its original format) here : http://adrock.ath.cx/img0061.ppm

    thanks again for the help,
    Matt.

  8. #23
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    i can deal with the whitespace layout (in fact my full program ignores it completely) and 'headerend' (in the full program) is generated by counting how much whitespace+header+comment the program ahs to deal with before the header ends.

    I was led to believe that with a .ppm file after the header file, the information has to be giving (usually from pixel top to bottom, left to right) in a uniform manner (i.e. after header no whitespace and no comments) I got this from http://astronomy.swin.edu.au/~pbourke/dataformats/ppm/ though I may be wrong (i frequently am :S).

  9. #24
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    int *ptr_data;
    Shouldn't it be an unsigned char*?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #25
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I can't access the server to get the file.

    The ppm file format is not 'binary' data, it is human-readable ASCII data and as such your attempts to read it as a binary chunk via fread is basically doomed to failure. You need to read in each pixel value into an integer via fscanf() or cin (depending on C vs C++ respectively) in some sort of loop. If I get time tonight (and I can get your file to download) or tomorrow morning if I don't get time tonight, I can put together a program based on the URL you supplied to read in the data.

    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. #26
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    If the .zip version isn't small enough, try .rar ing it with WinRAR. RAR is usually smaller than ZIP. And WinRAR can do both formats.

  12. #27
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mitakeet
    The ppm file format is not 'binary' data, it is human-readable ASCII data
    Or it isn't. From what I've read, it depends.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #28
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33
    Quote:
    Originally Posted by mitakeet
    The ppm file format is not 'binary' data, it is human-readable ASCII data
    yeah depending on what is set up in the header portion of the file, if it is in 'p3' format then it is ASCII but if it is in p6 format then its in simple byte format.
    If the PPM magic identifier is "P6" then the image data is stored in byte format, one byte per colour component (r,g,b).
    i will have alook at fscanf() to see what that returns. (it can't be any worse..... )

    cheers,
    matt
    Last edited by Giant; 06-22-2005 at 03:12 AM.

  14. #29
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Looking at the file (I was able to download it) it sure ain't ASCII. I will try to develop a program to read the file and store it in a structure for you.

    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

  15. #30
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    This works on Windows and on Solaris (Unix):

    Code:
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    /*
        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) {
        unsigned char *ptr_data;
        int i=0, height, width, color;
        FILE *temp_fptr;
        size_t totBytes, bytesRead;
        const char *fileName = "img0061.ppm"; /* get from the command line */
        char buf[BUFSIZ];
    
        temp_fptr = fopen(fileName,"rb");
        if (!temp_fptr){
            fprintf(stderr, "Can't open file!\n");
            exit(1);
        }
    
        /* get and check magic number */
        i=0;
        while (!isspace(buf[i] = fgetc(temp_fptr))){i++;}
        buf[i] = '\0';
        if (strncmp(buf, "P6", 2)){
            fprintf(stderr, "File's magic number is invalid! (only read P6 files)\n");
            exit(1);
        }
    
        /* get and check width */
        i=0;
        while (!isspace(buf[i] = fgetc(temp_fptr))){i++;}
        buf[i] = '\0';
        if (sscanf(buf, "%d", &width) != 1){
            fprintf(stderr, "Failed to read in width correctly!\n");
            exit(1);
        }
        if (width < 1 || width > 65536){ /* arbitrary upper limit, you can change this */
            fprintf(stderr, "Invalid or out-of-bounds value for width!\n");
            exit(1);
        }
    
        /* get and check height */
        i=0;
        while (!isspace(buf[i] = fgetc(temp_fptr))){i++;}
        buf[i] = '\0';
        if (sscanf(buf, "%d", &height) != 1){
            fprintf(stderr, "Failed to read in height correctly!\n");
            exit(1);
        }
        if (height < 1 || height > 65536){ /* arbitrary upper limit, you can change this */
            fprintf(stderr, "Invalid or out-of-bounds value for height!\n");
            exit(1);
        }
    
    
        /* get color (don't care about validity) */
        i=0;
        while (!isspace(buf[i] = fgetc(temp_fptr))){i++;}
        buf[i] = '\0';
        if (sscanf(buf, "%d", &color) != 1){
            fprintf(stderr, "Failed to read in color correctly!\n");
            exit(1);
        }
        totBytes = 3*width*height;
    
        ptr_data = (unsigned char *) malloc(totBytes);
        if (!ptr_data){
            fprintf(stderr, "Failed to allocate %d bytes of memory!\n", totBytes);
            exit(1);
        }
        bytesRead = fread(ptr_data, 1, (totBytes), temp_fptr);
    
        printf("\nbytesRead: %d\nFirst 10 bytes in hex:\n", bytesRead);
        for (i=0; i<10; i++){
            printf("%02X ", ptr_data[i]);
        }
        printf("\n");
    
        fclose(temp_fptr);
        free(ptr_data);
    
        return 0;
    }
    My output:


    bytesRead: 1045008
    First 10 bytes in hex:
    70 38 29 6A 3B 2A 59 31 27 51

    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 2 of 3 FirstFirst 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, 10:09 PM
  2. Binary file is read with bytes missing
    By thomas41546 in forum C# Programming
    Replies: 1
    Last Post: 09-03-2006, 05:15 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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