fprintf format specifiers

Discussion in 'C Programming' started by Harry, Oct 12, 2006.

  1. Harry

    Harry Guest

    Good Day,

    Is there a way to print 8 bytes in a line in a file using
    fprintf...like this

    12 76 89 76 86 34 98 08
    A4..............................
     
    Harry, Oct 12, 2006
    #1
    1. Advertising

  2. Harry said:

    > Good Day,
    >
    > Is there a way to print 8 bytes in a line in a file using
    > fprintf...like this
    >
    > 12 76 89 76 86 34 98 08
    > A4..............................


    Nope. But of course you can do something like this:

    int ch;
    int i = 0;
    while((ch = getc(fpin)) != EOF)
    {
    fprintf(fpout, " %02X", ch);
    if(++i % 8 == 0)
    {
    i = 0;
    putc('\n', fpout);
    }
    }
    if(i > 0)
    {
    putc('\n', fpout);
    }


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Oct 12, 2006
    #2
    1. Advertising

  3. Richard Heathfield wrote:
    > Harry said:
    >
    >> Good Day,
    >>
    >> Is there a way to print 8 bytes in a line in a file using
    >> fprintf...like this
    >>
    >> 12 76 89 76 86 34 98 08
    >> A4..............................

    >
    > Nope. But of course you can do something like this:

    Well - one could read 8 bytes, and do
    fprintf(fpout, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
    buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);

    Assuming he wanted a hexadecimal representation of each
    of the individual byte values.



    > int ch;
    > int i = 0;
    > while((ch = getc(fpin)) != EOF)
    > {
    > fprintf(fpout, " %02X", ch);
    > if(++i % 8 == 0)
    > {
    > i = 0;
    > putc('\n', fpout);
    > }
    > }
    > if(i > 0)
    > {
    > putc('\n', fpout);
    > }
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Oct 12, 2006
    #3
  4. "Nils O. SelÄsdal" said:

    > Richard Heathfield wrote:
    >> Harry said:
    >>
    >>> Good Day,
    >>>
    >>> Is there a way to print 8 bytes in a line in a file using
    >>> fprintf...like this
    >>>
    >>> 12 76 89 76 86 34 98 08
    >>> A4..............................

    >>
    >> Nope. But of course you can do something like this:

    > Well - one could read 8 bytes, and do
    > fprintf(fpout, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
    > buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);


    ....er, oh yeah, so he can. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Oct 12, 2006
    #4
  5. Harry

    CBFalconer Guest

    Richard Heathfield wrote:
    > Harry said:
    >
    > > Is there a way to print 8 bytes in a line in a file using
    > > fprintf...like this
    > >
    > > 12 76 89 76 86 34 98 08
    > > A4..............................

    >
    > Nope. But of course you can do something like this:
    >
    > int ch;
    > int i = 0;
    > while((ch = getc(fpin)) != EOF)
    > {
    > fprintf(fpout, " %02X", ch);
    > if(++i % 8 == 0)
    > {
    > i = 0;
    > putc('\n', fpout);
    > }
    > }
    > if(i > 0)
    > {
    > putc('\n', fpout);
    > }


    How about:

    char b[8];
    ...
    fprintf(fpout, "%02x", b[0]);
    for (i = 1, i < 8; i++) fprintf(fpout, " %02x", b);
    putc(fpout, '\n');

    or

    fprintf(fpout, "%02x %02x %02x %02x %02x %02x %02x %02x\j",
    b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);

    --
    Some informative links:
    <news:news.announce.newusers
    <http://www.geocities.com/nnqweb/>
    <http://www.catb.org/~esr/faqs/smart-questions.html>
    <http://www.caliburn.nl/topposting.html>
    <http://www.netmeister.org/news/learn2quote.html>
    <http://cfaj.freeshell.org/google/>
     
    CBFalconer, Oct 12, 2006
    #5
  6. Richard Heathfield <> wrote:

    > > Well - one could read 8 bytes, and do
    > > fprintf(fpout, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
    > > buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);


    > ...er, oh yeah, so he can. :)


    Yes, but what about the (likelier) case where the file has a number of
    bytes that isn't a multiple of 8? Your original version handled that
    easily; the suggested version less so. I'm presuming, possibly
    incorrectly, that fprintf() is expensive relative to sprintf(), so
    perhaps something like this would be good (criticisms welcome):

    #include <stdio.h>
    #include <assert.h>

    #define BYTES_PER_LINE 8

    int main( int argc, char *argv[] )
    {
    char outbuf[3*BYTES_PER_LINE+1], *bufptr;
    int inchar, count=0;
    FILE *fpin, *fpout;

    assert( argc > 2 );
    assert( (fpin = fopen(argv[1],"r")) != NULL );
    assert( (fpout = fopen(argv[2],"w")) != NULL );
    bufptr = outbuf;
    while( (inchar=fgetc(fpin)) != EOF ) {
    bufptr += sprintf( bufptr, "%02X ", inchar ); /* assume success */
    if( ++count % BYTES_PER_LINE == 0 ) {
    *(bufptr-1)='\n';
    fputs( outbuf, fpout );
    bufptr = outbuf;
    }
    }
    if( bufptr != outbuf ) {
    *(bufptr-1)='\n';
    fputs( outbuf, fpout );
    }
    assert( !fclose(fpin) );
    assert( !fclose(fpout) );
    return 0;
    }

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 12, 2006
    #6
  7. Christopher Benson-Manica said:

    > Richard Heathfield <> wrote:
    >
    >> > Well - one could read 8 bytes, and do
    >> > fprintf(fpout, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
    >> > buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);

    >
    >> ...er, oh yeah, so he can. :)

    >
    > Yes, but what about the (likelier) case where the file has a number of
    > bytes that isn't a multiple of 8? Your original version handled that
    > easily;


    Naturellement...

    > the suggested version less so.


    ....mais, m'sieur, le question originale ne called pas for une solution
    generale. Il merely required pour huit bytes to be ecri a stdout, oui?

    > I'm presuming, possibly
    > incorrectly, that fprintf() is expensive relative to sprintf(), so
    > perhaps something like this would be good (criticisms welcome):
    >
    > #include <stdio.h>
    > #include <assert.h>
    >
    > #define BYTES_PER_LINE 8
    >
    > int main( int argc, char *argv[] )
    > {
    > char outbuf[3*BYTES_PER_LINE+1], *bufptr;


    I'd be tempted to make the "bytes per line" value configurable at runtime.

    > int inchar, count=0;
    > FILE *fpin, *fpout;
    >
    > assert( argc > 2 );
    > assert( (fpin = fopen(argv[1],"r")) != NULL );
    > assert( (fpout = fopen(argv[2],"w")) != NULL );


    Three consecutive poor uses of assert. (I suspect you know this. I'm merely
    heads-upping for newbies' benefit.)

    > bufptr = outbuf;
    > while( (inchar=fgetc(fpin)) != EOF ) {
    > bufptr += sprintf( bufptr, "%02X ", inchar ); /* assume success */
    > if( ++count % BYTES_PER_LINE == 0 ) {
    > *(bufptr-1)='\n';


    Does that overwrite the null terminator? I haven't checked, and perhaps it
    doesn't, but the safety of the code is not obvious at a (cursory) glance.

    > fputs( outbuf, fpout );
    > bufptr = outbuf;
    > }
    > }
    > if( bufptr != outbuf ) {
    > *(bufptr-1)='\n';
    > fputs( outbuf, fpout );
    > }
    > assert( !fclose(fpin) );
    > assert( !fclose(fpout) );


    Two more poor uses of assert. If you must do this, spin them around. If you
    can't close the input file, it would be good to at least have had a stab at
    closing the output file before the abort.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Oct 12, 2006
    #7
  8. "Christopher Benson-Manica" <> wrote in message
    news:eglfn4$smh$...
    > Richard Heathfield <> wrote:
    >
    > > > Well - one could read 8 bytes, and do
    > > > fprintf(fpout, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
    > > > buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);

    >
    > > ...er, oh yeah, so he can. :)

    >
    > Yes, but what about the (likelier) case where the file has a number of
    > bytes that isn't a multiple of 8? Your original version handled that
    > easily; the suggested version less so. I'm presuming, possibly
    > incorrectly, that fprintf() is expensive relative to sprintf(), so
    > perhaps something like this would be good (criticisms welcome):
    >


    In that case, you'd use fread() with the size of a fixed buffer. fread()
    will return the quantity read. This will either be the size of the buffer,
    or a smaller quantity. So, you need two printing loops: one for the entire
    buffer, and one for the remaining bytes. Both of which have been shown.


    Rod Pemberton
     
    Rod Pemberton, Oct 13, 2006
    #8
  9. On Fri, 13 Oct 2006, Rod Pemberton wrote:

    >
    > "Christopher Benson-Manica" <> wrote in message
    > news:eglfn4$smh$...
    >> Richard Heathfield <> wrote:
    >>
    >>>> Well - one could read 8 bytes, and do
    >>>> fprintf(fpout, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
    >>>> buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7]);

    >>
    >>> ...er, oh yeah, so he can. :)

    >>
    >> Yes, but what about the (likelier) case where the file has a number of
    >> bytes that isn't a multiple of 8? Your original version handled that
    >> easily; the suggested version less so. I'm presuming, possibly
    >> incorrectly, that fprintf() is expensive relative to sprintf(), so
    >> perhaps something like this would be good (criticisms welcome):
    >>

    >
    > In that case, you'd use fread() with the size of a fixed buffer. fread()
    > will return the quantity read. This will either be the size of the buffer,
    > or a smaller quantity. So, you need two printing loops: one for the entire
    > buffer, and one for the remaining bytes. Both of which have been shown.


    In theory one could also use Duff's device:

    int n = (count + 7) / 8;

    switch (count % 8) {
    case 0: do { printf("%02X ", *from++);
    case 7: printf("%02X ", *from++);
    case 6: printf("%02X ", *from++);
    case 5: printf("%02X ", *from++);
    case 4: printf("%02X ", *from++);
    case 3: printf("%02X ", *from++);
    case 2: printf("%02X ", *from++);
    case 1: printf("%02X ", *from++);
    putchar('\n');
    } while (--n > 0);
    }

    Tak-Shing
     
    Tak-Shing Chan, Oct 13, 2006
    #9
  10. Richard Heathfield <> wrote:

    > ...mais, m'sieur, le question originale ne called pas for une solution
    > generale. Il merely required pour huit bytes to be ecri a stdout, oui?


    Ah, le Francais, le langue d'amour! It sounds like you remember as
    much of it as I do ;-) (Tu as raison.)

    > I'd be tempted to make the "bytes per line" value configurable at runtime.


    A rare time to give oneself over to the seductive whisperings of
    temptation with nary a pang of guilt...

    > Three consecutive poor uses of assert. (I suspect you know this. I'm merely
    > heads-upping for newbies' benefit.)


    Right, they were quick cover-my-bases hacks.

    > > while( (inchar=fgetc(fpin)) != EOF ) {
    > > bufptr += sprintf( bufptr, "%02X ", inchar ); /* assume success */
    > > if( ++count % BYTES_PER_LINE == 0 ) {
    > > *(bufptr-1)='\n';


    > Does that overwrite the null terminator? I haven't checked, and perhaps it
    > doesn't, but the safety of the code is not obvious at a (cursory) glance.


    No, bufptr points to the null terminator, assuming per the comment
    that sprintf() doesn't do anything unexpected; the code overwrites
    the last space in the string. The fact that it wasn't obvious to
    you probably means it isn't the best plan, but it does work.

    > Two more poor uses of assert. If you must do this, spin them around. If you
    > can't close the input file, it would be good to at least have had a stab at
    > closing the output file before the abort.


    Agreed, thank you.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 13, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    0
    Views:
    654
  2. Ben Pfaff

    Re: C# format Specifiers Help.

    Ben Pfaff, Aug 21, 2003, in forum: C Programming
    Replies:
    0
    Views:
    416
    Ben Pfaff
    Aug 21, 2003
  3. Martin Ambuhl

    Re: C# format Specifiers Help.

    Martin Ambuhl, Aug 21, 2003, in forum: C Programming
    Replies:
    4
    Views:
    512
    Randy Howard
    Aug 22, 2003
  4. Jeff

    Re: C# format Specifiers Help.

    Jeff, Aug 21, 2003, in forum: C Programming
    Replies:
    0
    Views:
    475
  5. Peter Ammon

    Writing arguments under format specifiers

    Peter Ammon, Jul 4, 2004, in forum: C Programming
    Replies:
    4
    Views:
    324
    Mike Wahler
    Jul 6, 2004
Loading...

Share This Page