I don't know which direction to go,perhaps something like reflection will help?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
The answer of @Kerrek SB works realy well, I just post how to use it in a function using a void pointer.
What do you want to do with your file once you've got it? If it's going to be read back in at a later time just use
fread
andfwrite
, likeThis will give binary output that's dependant on your compiler/platform, as long as those are unchanged you should be fine. From there you can also feed the file into a hex reader etc., though you'll need to know the layout of the struct to do anything useful with it.
Here's a hex dump, about as general as you can get:
Note that the result of this is entirely implementation-defined; presumably there'll be plenty of padding, and you won't know what any of the printed values mean. (Most of them will probably just be pointers to some other part of space.)
As Etienne says, C is a statically typed language and does not have reflection, so you have to know the declaration of
Foo
in order to interpret the content ofx
.