How do I dump an arbitrary struct in C?

后端 未结 4 1104
日久生厌
日久生厌 2021-01-12 03:07

I don\'t know which direction to go,perhaps something like reflection will help?

4条回答
  •  耶瑟儿~
    2021-01-12 03:52

    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 and fwrite, like

    struct foo * bar;
    fwrite(bar,sizeof(*bar),1,stdout);
    
    ...
    
    fread(bar,sizeof(*bar),1,stdin);
    

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

提交回复
热议问题