Print out a buffer

This is a discussion on Print out a buffer within the C Programming forums, part of the General Programming Boards category; So, I've got this buffer that I want to print out byte by byte. When the buffer contains a string, ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    11

    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...

    <snip>
    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" );

    </snip>

    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?

    Thanks,

    B

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    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:
    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

    B

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, 11:05 AM
  3. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02: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

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