473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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?


Mar 3 '08 #1
2 2393
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.
Mar 3 '08 #2
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.

Mar 4 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: uli | last post by:
Hi all! I'm posting to both newsgroups, because it's actually a C++ problem but could be that some of you using Matlab-&-MEX-&-C++ was struggling with the same problem. I'm trying to rewrite...
0
by: Richard Taylor | last post by:
User-Agent: OSXnews 2.07 Xref: number1.nntp.dca.giganews.com comp.lang.python:437315 Hi I am trying to use py2app (http://undefined.org/python/) to package a gnome-python application...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
6
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being...
3
by: ehabaziz2001 | last post by:
I know one of the pointer benefit is that we can return more than one value from a function . The down program has an error that I can not discover . That is the call of the function : The Call:...
3
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h>...
1
by: verge | last post by:
hello everyone! how's it going? like everyone in here im in need of some help and good friendship along the way...take a look at this: //MODIFIED SO IT DEALS WITH WINDOWS FTP USING ACTIVE...
0
hqprog
by: hqprog | last post by:
Having search extensively I've learned the two functions timegm and gmtime_r though in the GNU standard C library extend the ISO standard. I need to use these two functions in myprog.c (on pc - ...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.