Using printf to output the contents of a struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • benn

    Using printf to output the contents of a struct

    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?






  • Vladimir Oka

    #2
    Re: Using printf to output the contents of a struct

    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.

    Comment

    • santosh

      #3
      Re: Using printf to output the contents of a struct

      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.

      Comment

      Working...