Thread: Writing and Reading unit32_t values using htons and ntohs

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    2

    Writing and Reading unit32_t values using htons and ntohs

    Hey there,

    I'm having an issue with writing uint32_t values to file and then reading them back.

    Code:
    //My code to write the value is as follows,
    
    //eklen = 128
    
    FILE* pFilOutput = open_memstream(pChrOutputBuffer, &pSizTotalSize);
    uint32_t eklen_n = htonl(eklen);
    if (fwrite(&eklen_n, sizeof(uint32_t), 1, pFilOutput) != 1) {
       //Failed to write data
    }
    
    //My code to read the value back again is as follows,
    
    FILE* pFilInput = fmemopen(pChrData, strlen(pChrData), "r");
    uint32_t eklen_n;
    fread(&eklen_n, sizeof(eklen_n), 1, pFilInput);
    int eklen = ntohl(eklen_n);
    Wrote = -2147483648
    Read 1344601458

    ---

    I'm guessing I'm making a school boy error here but I just can't see it. I thought maybe I needed to specify "b" in the fmemopen mode parameter but that didn't help and on further reading I assume it's binary by default anyway.

    Any ideas what I'm doing wrong? Many thanks in advance for any help!

    Nick.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Are you checking your fread() call for success?

    This doesn't look like a problem with htonl/ntohl, since the values aren't the byte-reversed values of one another.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, the problem is not your use of htonl, ntohl, fread, or fwrite. Have you tried this with a straight sequence of bytes to check if fmemopen and open_memstream are doing what you think they are?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    2
    Quote Originally Posted by JohnGraham View Post
    Are you checking your fread() call for success?

    This doesn't look like a problem with htonl/ntohl, since the values aren't the byte-reversed values of one another.
    Hey John, yup, I've added checks for that and it's returning without error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. htons and ntohs
    By nabi in forum C Programming
    Replies: 2
    Last Post: 08-26-2010, 03:59 AM
  2. writing an array of bit-values
    By damansternpak in forum C Programming
    Replies: 2
    Last Post: 07-16-2007, 11:31 PM
  3. Use of htons() and ntohs()
    By Andystudent in forum C Programming
    Replies: 4
    Last Post: 01-19-2006, 10:06 AM
  4. Htons?
    By ew16301 in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-17-2005, 05:29 PM
  5. writing variable (int and string) values...
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 06-18-2002, 12:58 PM

Tags for this Thread