newbie problems with write()

This is a discussion on newbie problems with write() within the C Programming forums, part of the General Programming Boards category; Hi I have some problems with the write function (atleast I think that is the problem). The following code should ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    3

    newbie problems with write()

    Hi I have some problems with the write function (atleast I think that is the problem). The following code should when executed output (to file png.png) the following string (hex) 89 50 4e 47 0d 0a 1a 0a however it adds 0d (13) infront of the 0a (10) for some strange reason that I cant figure out.
    now it looks like this 89 50 4e 47 0d 0d 0a 1a 0d0a.
    here is the code


    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>

    unsigned char png_magic[8] = {137, 80, 78, 71, 13, 10, 26, 10};

    int main()
    {
    int fd;
    fd = open( "png.png" , O_RDWR | O_CREAT | O_TRUNC , 0644 );

    write(fd, &png_magic, sizeof(png_magic));
    close(fd);

    return 0;
    }

    I have tried both lcc and the bloodshed devcpp compiler on winxp and win98 both output code that generates the same fault.
    please help.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This is just a hunch, but the 0a character is probably some special character that your OS needs to use 2 characters to represent in text mode. (For example, DOS represents '\n' with 2 character codes). Try setting your open command to binary mode, instead of text mode.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    This may be of little conciliation, but your code works as expected on Linux (gcc version 2.95.3 20010315 (release)).
    Jason Deckard

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Try
    fd = open( "png.png" , O_RDWR | O_CREAT | O_TRUNC | O_BINARY , 0644 );

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    3
    Ok I have set the file to binary. but now the hex string looks like this 89 50 4e 47 0d 0a 1a 0d 0a. so either it "respects" that there is already a 0d or it is overwriting it, but only in the first occasion very odd (I think). I will do some experimenting placing 10 and 13 in different patters.I will report back. thanks for the help.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    3
    nah disregard the last post, I must have accidentaly deleted a byte when I was hitting refresh in my hex editor. The program now works thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  2. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 09:25 PM
  3. Text analysis problems - need help!
    By haz115 in forum C Programming
    Replies: 1
    Last Post: 04-14-2002, 08:27 AM
  4. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 06:53 PM

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