Writing PGM header info to .pgm file

This is a discussion on Writing PGM header info to .pgm file within the C Programming forums, part of the General Programming Boards category; I am having trouble writing the PGM header info to a new pgm file. When i do a hex dump ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    20

    Writing PGM header info to .pgm file

    I am having trouble writing the PGM header info to a new pgm file. When i do a hex dump of the first 20 bytes of the original and new header, i can see that "P5" is correct in both the original and new files, after that the, the original file has the width, heigh and maxgrey values set. The new file does not have the correct values set, it just reads as "." on the hex dump. Should i be using fwrite() to output the header data or fprintf()? If i should be using fwrite, should i be using individual fwrite() statements for each piece of the header or can i just have a pointer at the beginning of my struct (i put the original header data into a struct) and fwrite() from there?

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    If it's a binary file (seems likely if you're using a hex dump to read it), then you need to use fwrite (or fputc), and probably use "wb" mode as well (especially if you're on windows).

    You shouldn't try to write a struct, unless you know for sure that you have all the padding and alignment issues sorted out (this isn't easy, or portable).

    Even if you have say
    Code:
    unsigned short width; 
    fwrite(&width,sizeof(width),1,fp);
    can have problems with endian

    The only truly safe and portable way is something like
    Code:
    fputc( width & 0xFF, fp );  // The LSB
    fputc( ( width >> 8 ) & 0xff, fp );  // The MSB
    Google "padding and alignment" and "endian".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dford425 View Post
    I am having trouble writing the PGM header info to a new pgm file. .
    Do I understand correctly?

    You are writing stuff to an executable file... That is some serious bad JuJu, just begging for all kinds of problems.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Quote Originally Posted by Salem View Post
    If it's a binary file (seems likely if you're using a hex dump to read it), then you need to use fwrite (or fputc), and probably use "wb" mode as well (especially if you're on windows).

    You shouldn't try to write a struct, unless you know for sure that you have all the padding and alignment issues sorted out (this isn't easy, or portable).

    Even if you have say
    Code:
    unsigned short width; 
    fwrite(&width,sizeof(width),1,fp);
    can have problems with endian

    The only truly safe and portable way is something like
    Code:
    fputc( width & 0xFF, fp );  // The LSB
    fputc( ( width >> 8 ) & 0xff, fp );  // The MSB
    Google "padding and alignment" and "endian".
    I have the struct to read in the header info from the original file and to store it. I will read up about padding and alignment to see if that helps. Thanks.

    Quote Originally Posted by CommonTater View Post
    Do I understand correctly?

    You are writing stuff to an executable file... That is some serious bad JuJu, just begging for all kinds of problems.
    .pgm is an image file. Right now i read in an image file and im trying to write the same header info from that into a new .pgm file.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dford425 View Post
    .pgm is an image file.
    Ahhh... ok.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    I got it to work using fprintf(). The problem was that there were newline characters inbetween some of the values

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM

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