Thread: Can't save file! [NULL Byte termination]

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    10

    Can't save file! [NULL Byte termination]

    Hi!
    I writed a simple server, that sends an ELF file everytime someone connects to it. But the problem is that this file containts NULL bytes:
    Code:
    char code[947]={ 127, 69, 76, 70, 1, 1, 1, 0, 0..
    The recv(); returns 947. Thats the size of the ELF file. But the problem is when I try to save this file. I try to save it like this:
    Code:
    recv(sock_fd, buffer, sizeof(buffer..
    FILE *fd;
    fd = fopen("DATA", "w");
    fprintf(fd, buffer);
    close(fd)
    But it only saves the first 7 bytes, and ends on NULL character. Is there any way to ignore NULL termination? I also try to save it like this:
    Code:
    FILE *fd;
    fd = fopen("DATA", "a");
    int i;
    for(i=0; i<recv_data_size; i++){
         fprintf(fd, &buffer[i]);
    }
    close(fd);
    But then data somehow repeats:
    Code:
    .ELF...ELF...LF...F........€€4€44444 (
    There are double ELF's and 4 F's, but there should only be one! Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fd = fopen("DATA", "w");
    > fprintf(fd, buffer);
    1. Use "wb" if you want to write a binary file.
    2. Use fwrite (not fprintf) to write binary data.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Thanks, that worked! But why this doesn't work with reading binary files!?
    I try it like this:
    Code:
    fd = fopen("pass", "rb");
    fread(buffer, 13790, 1, fd);
    printf("strlen: %i\n", strlen(buffer));
    It reads only first 14 bytes. The 15th character is NULL. Why doesn't ir read, I opened it as "rb", but still it reads only until it reaches NULL character!

    I have another question! I created a loop, where client recives data, and with strcmp the recived data is compared with the old data, so it fits all in one big buffer. But the problem is that after the compering, the big buffer is only 140 bytes long, it should be 13790. Here is the code:
    Code:
    char buffer[65535];
    char data[65545];
    
    while(1){
         recv_data_size = recv(sock_fd, data, sizeof(data), 0);
         strncat(buffer, data, recv_data_size);
         memset(data, 0, sizeof(data);
    }
    
    printf("size: %i\n", strlen(buffer));  //140 bytes
    Thank you!

  4. #4
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Salem
    2. Use fwrite (not fprintf) to write binary data.
    why is fwrite better than fprintf ?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    fread, fwrite - binary stream input/output
    fprintf, printf, sprintf.... - formatted output conversion

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > It reads only first 14 bytes
    Because you used a string function to find out the length - remember, it's binary data.
    Use the return result of fread (which you're currently ignoring) to find out how much data you read.

    > why is fwrite better than fprintf ?
    It's different, not better.

    > and with strcmp the recived data
    YET more string functions on binary data.
    Use memcmp() if you want to compare binary data!
    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.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    Is there a function like strcat(), but only with binary data?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, it's called memcpy
    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.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... or maybe memmove() if you're arrays overlap.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM