Using printf to output the contents of a struct

Discussion in 'C Programming' started by benn, Mar 3, 2008.

  1. benn

    benn Guest

    I need to output the contents of a struct variable (which internally
    has a bunch of ints, strings, other structs, etc) onto the screen
    (then ultimately out the com port). I don't mind a raw binary hex
    dump of the entire memory space that the variable occupies. I can
    then parse it on the other end.

    Whats the best way to do this? I can do a sizeof the struct variable
    and for-loop to print a hex character for every byte until all the
    memory that the variable occupies is cycled through, but I'm thinking
    theres gotta be a cleaner approach, no?
     
    benn, Mar 3, 2008
    #1
    1. Advertisements

  2. benn

    Vladimir Oka Guest

    benn wrote:
    > I need to output the contents of a struct variable (which internally
    > has a bunch of ints, strings, other structs, etc) onto the screen
    > (then ultimately out the com port). I don't mind a raw binary hex
    > dump of the entire memory space that the variable occupies. I can
    > then parse it on the other end.
    >
    > Whats the best way to do this? I can do a sizeof the struct variable
    > and for-loop to print a hex character for every byte until all the
    > memory that the variable occupies is cycled through, but I'm thinking
    > theres gotta be a cleaner approach, no?


    A cleaner approach is to use standard format specifiers and
    standard text output functions to produce data as well-defined
    and structured text that can then be similarly parsed "at the
    other end". That way you can make both programs, and the
    intermediate file portable. Internal representation of the
    structure is not.

    --
    My e-mail address is real, and I read it.
     
    Vladimir Oka, Mar 3, 2008
    #2
    1. Advertisements

  3. >I need to output the contents of a struct variable (which internally
    >has a bunch of ints, strings, other structs, etc) onto the screen
    >(then ultimately out the com port). I don't mind a raw binary hex
    >dump of the entire memory space that the variable occupies. I can
    >then parse it on the other end.


    Assuming you really don't mind raw binary (what is raw binary hex?
    Is that anything like a FORTRAN formatted unformatted record?):

    FILE *f;
    int ret;
    struct whatever struct_variable;
    /* open stream f for output here in binary mode */
    /* initialize struct_variable to something useful */

    ret = fwrite(&struct_variable, sizeof(struct_variable), 1, f);
    /* check if it worked */

    >Whats the best way to do this? I can do a sizeof the struct variable
    >and for-loop to print a hex character for every byte until all the
    >memory that the variable occupies is cycled through, but I'm thinking
    >theres gotta be a cleaner approach, no?


    Depending on the output format you require, that will work also.
     
    Gordon Burditt, Mar 3, 2008
    #3
  4. benn

    santosh Guest

    Vladimir Oka wrote:

    > benn wrote:
    >> I need to output the contents of a struct variable (which internally
    >> has a bunch of ints, strings, other structs, etc) onto the screen
    >> (then ultimately out the com port). I don't mind a raw binary hex
    >> dump of the entire memory space that the variable occupies. I can
    >> then parse it on the other end.
    >>
    >> Whats the best way to do this? I can do a sizeof the struct variable
    >> and for-loop to print a hex character for every byte until all the
    >> memory that the variable occupies is cycled through, but I'm thinking
    >> theres gotta be a cleaner approach, no?

    >
    > A cleaner approach is to use standard format specifiers and
    > standard text output functions to produce data as well-defined
    > and structured text that can then be similarly parsed "at the
    > other end". That way you can make both programs, and the
    > intermediate file portable. Internal representation of the
    > structure is not.


    You'd still have problems with pointer members. There's also the
    question of whether to output what those pointers point to or not.
     
    santosh, Mar 4, 2008
    #4
  5. benn

    Vladimir Oka Guest

    santosh wrote:
    > You'd still have problems with pointer members. There's also the
    > question of whether to output what those pointers point to or not.


    Well, OP did not specify whether he has them or what to do with
    them. I guess he'd want to print whatever is pointed to, unless
    the pointer members are just house-keeping ones (a linked
    list?). In that case they can probably just be ignored or just
    place-holders passed? But, the same problem is there even if he
    goes for memory dump.


    --
    My e-mail address is real, and I read it.
     
    Vladimir Oka, Mar 4, 2008
    #5
  6. benn

    benn Guest

    On Mar 3, 3:43 pm, Vladimir Oka <> wrote:
    > benn wrote:
    > > I need to output the contents of a struct variable (which internally
    > > has a bunch of ints, strings, other structs, etc) onto the screen
    > > (then ultimately out the com port). I don't mind a raw binary hex
    > > dump of the entire memory space that the variable occupies. I can
    > > then parse it on the other end.

    >
    > > Whats the best way to do this? I can do a sizeof the struct variable
    > > and for-loop to print a hex character for every byte until all the
    > > memory that the variable occupies is cycled through, but I'm thinking
    > > theres gotta be a cleaner approach, no?

    >
    > A cleaner approach is to use standard format specifiers and
    > standard text output functions to produce data as well-defined
    > and structured text that can then be similarly parsed "at the
    > other end". That way you can make both programs, and the
    > intermediate file portable. Internal representation of the
    > structure is not.
    >
    > --
    > My e-mail address is real, and I read it.


    Thanks, this does sound cleaner! And fortunately, the structure
    doesn't contain any pointers other than a few null terminated
    character arrays.

    The only downside I see with the file portable approach, is the
    overhead associated with putting in tags for each field. I'm
    assuming you mean like an XML format, which Im not too familiar with,
    but doesnt it have ascii tags that go before the raw data (binary or
    ascii)? This status outputter is actually going to run on an
    embedded pc with very limited resources, so the time it takes to
    transfer (115bps) this block of data should be as minimal as
    possible.
     
    benn, Mar 5, 2008
    #6
  7. benn

    Vladimir Oka Guest

    benn wrote:
    > The only downside I see with the file portable approach, is the
    > overhead associated with putting in tags for each field. I'm
    > assuming you mean like an XML format, which Im not too familiar with,
    > but doesnt it have ascii tags that go before the raw data (binary or
    > ascii)? This status outputter is actually going to run on an
    > embedded pc with very limited resources, so the time it takes to
    > transfer (115bps) this block of data should be as minimal as
    > possible.
    >


    If this is to be used in a very limited environmet XML is
    certinaly an overkill. You can always design your own efficient
    format. As long as it's well documented it's going to be
    maintainable.
     
    Vladimir Oka, Mar 5, 2008
    #7
    1. Advertisements

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. Chris Fogelklou
    Replies:
    36
    Views:
    1,575
    Chris Fogelklou
    Apr 20, 2004
  2. ben
    Replies:
    4
    Views:
    734
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,468
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    533
  5. guru
    Replies:
    8
    Views:
    383
Loading...

Share This Page