Using printf to output the contents of a struct

B

benn

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?
 
V

Vladimir Oka

benn said:
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.
 
G

Gordon Burditt

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

santosh

Vladimir said:
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.
 
V

Vladimir Oka

santosh said:
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.
 
B

benn

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.

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

Vladimir Oka

benn said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top