Thread: Print out a buffer

  1. #1
    Registered User
    Join Date
    Aug 2006

    Print out a buffer

    So, I've got this buffer that I want to print out byte by byte. When the buffer contains a string, everthing works just fine, but then I want to encode the buffer, and print it out again. I don't care if a byte doesn't correspond to a good looking ascii character, I just want to see what I've got. For some reason though, I can't seem to print out the whole thing.

    Here is what i've got...

    char *buffer = malloc ( 520 );
    unsigned short *bufferLen = malloc ( sizeof (short) );
    int i;

    strcpy ( buffer, "This is buffer");
    *bufferLen = (unsigned short) strlen ( buffer );

    printf ( "%s", "Buffer -->" );

    for ( i = 0; i < *bufferLen; i++ )
    putc( buffer[i], stdout );

    printf ( "%s", "<--\n\n" );


    And it works fine... but as soon as I've got some wierd bytes in my buffer, it will stop printing to screen... I even lose the ending "<--"!!!! When I decode and print... everything is good again, so I know that I am not loosing my information... I just can't print it!!

    Any suggestions?



  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    First of all, use code tags before someone neuters you.

    Second of all, you should avoid printing non-printable characters because it can really screw up your terminal session. Try this instead for your loop:
    for ( i = 0; i < *bufferLen; i++ )
      putc( isprint(buffer[i]) ? buffer[i] : '.' , stdout );
    That way it will just print a '.' for non-printable characters. Make sure you #include <ctype.h> for the isprint() function.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > I don't care if a byte doesn't correspond to a good looking ascii character
    But your terminal driver will.
    You can't just output any old character, especially the control characters from 0 to 31.
    Also printf() will simply stop at a \0, if that's the result of your encoding of a particular character.
    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.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    *bufferLen = (unsigned short) strlen ( buffer );
    BTW strlen returns a size_t.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Thanks guys! I didn't know about the "isprint()" function. That clears things up nicely!


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Frame buffer not working
    By Noise in forum Game Programming
    Replies: 1
    Last Post: 02-15-2009, 12:05 PM
  3. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  4. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  5. How Can I print
    By kas2002 in forum C++ Programming
    Replies: 2
    Last Post: 06-22-2002, 10:07 AM