Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Using printf to output the contents of a struct (http://www.velocityreviews.com/forums/t595828-using-printf-to-output-the-contents-of-a-struct.html)

benn 03-03-2008 11:10 PM

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 03-03-2008 11:43 PM

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.

Gordon Burditt 03-03-2008 11:49 PM

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


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.


santosh 03-04-2008 07:54 AM

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.


Vladimir Oka 03-04-2008 09:05 AM

Re: Using printf to output the contents of a struct
 
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.

benn 03-05-2008 02:22 AM

Re: Using printf to output the contents of a struct
 
On Mar 3, 3:43 pm, Vladimir Oka <vladimir....@btopenworld.com> 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.



Vladimir Oka 03-05-2008 04:41 PM

Re: Using printf to output the contents of a struct
 
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.


All times are GMT. The time now is 06:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.