multiple structures in a file c -
i need save file contains multiple structure data not in defined order.
sample structs:
typedef struct { int a; char b; } structa_t; typedef struct { custom_t a; char *b; } structb_t; typedef struct { long a; short b; } structc_t;
the file contents struct:
typedef struct { int istructtype; // how can this?? void *vpstructdatas; char *pnextstruct; // not necessary, can calculated sizeof() } filestruct_t;
sample file content:
[structa identifier][structa data][structc identifier][structc data]
or:
[structb identifier][structb data]
every combination (even multiple definitions of type) needs possible using this.
like this, want save , read multiple structures in file dynamically.
can achieved?
update: if use struct labels id's or names,
how may write real struct file, not pointer , after reading, how should act? need create these read data structs globally in memory?
to prevent misunderstanding, i'm going save data structures contain file. not struct definition.
i didn't realise there wiki page thing referring earlier...
https://en.wikipedia.org/wiki/x_macro
basically technique allow "standardise" way add new structures application without having write same kinds of functions on , on again. same macros define struct can re-defined in order perform read/write structs files.
conceptually you'd end #including same header file @ least 3 times each time change meaning of macros called in header file. if harnessed correctly can extremely powerful way simplify large amounts of code otherwise prone human error maintain.
what follows example program shows how create "x macros" standardise interfacing many structures share similar theme.
the "print_..." functions defined here analogous functions read , write each kind of structure file in program.
file: xm_struct_definers.h
#define xm_struct_header(xm_name_xm) typedef struct xmacros_##xm_name_xm xm_name_xm; \ struct xmacros_##xm_name_xm { #define xm_struct_member_int(xm_name_xm) int xm_name_xm; #define xm_struct_member_char(xm_name_xm) char xm_name_xm; #define xm_struct_member_char_star(xm_name_xm) char* xm_name_xm; #define xm_struct_member_long(xm_name_xm) long xm_name_xm; #define xm_struct_member_short(xm_name_xm) short xm_name_xm; #define xm_struct_member_custom_t(xm_name_xm) custom_t xm_name_xm; #define xm_struct_footer };
file: xm_structs.h
//these "x-macro" calls! //they defined , re-defined in multiple places in program. //do not put "normal" c code in file. xm_struct_header(structa_t) xm_struct_member_int(a) xm_struct_member_char(b) xm_struct_footer xm_struct_header(structb_t) xm_struct_member_custom_t(a) xm_struct_member_char_star(b) xm_struct_footer xm_struct_header(structc_t) xm_struct_member_long(a) xm_struct_member_short(b) xm_struct_footer
file: xm_clear_macros.h
#undef xm_struct_header #undef xm_struct_member_int #undef xm_struct_member_char #undef xm_struct_member_char_star #undef xm_struct_member_long #undef xm_struct_member_short #undef xm_struct_member_custom_t #undef xm_struct_footer
file: xm_struct_printers.h
#define xm_struct_header(xm_name_xm) void print_##xm_name_xm(xm_name_xm* record) {\ printf("--struct '"#xm_name_xm"'\n"); #define xm_struct_member_int(xm_name_xm) printf("int " #xm_name_xm ": %d\n", record->xm_name_xm); #define xm_struct_member_char(xm_name_xm) printf("char " #xm_name_xm ": %c\n", record->xm_name_xm); #define xm_struct_member_char_star(xm_name_xm) printf("char* " #xm_name_xm ": %s\n", record->xm_name_xm); #define xm_struct_member_long(xm_name_xm) printf("long " #xm_name_xm ": %ld\n", record->xm_name_xm); #define xm_struct_member_short(xm_name_xm) printf("short " #xm_name_xm ": %hd\n", record->xm_name_xm); #define xm_struct_member_custom_t(xm_name_xm) printf("custom_t "#xm_name_xm":\n\t.x: %d\n\t.y: %d\n\t.z: %d\n" \ , record->xm_name_xm.x, record->xm_name_xm.y, record->xm_name_xm.z); #define xm_struct_footer printf("--end of struct\n\n");\ }
file: main.c
#include <stdio.h> typedef struct { int x; int y; int z; } custom_t; //this first usage of x macros... //these create definitions of structures #include "xm_struct_definers.h" #include "xm_structs.h" #include "xm_clear_macros.h" //------------------------------ //this second usage of x macros... //which creates 3 functions print out contents of each type of structure #include "xm_struct_printers.h" #include "xm_structs.h" #include "xm_clear_macros.h" //------------------------------ int main() { structa_t sa; sa.a = 123; sa.b = 'a'; structb_t sb; sb.a.x = 1; sb.a.y = 2; sb.a.z = 3; sb.b = "bbbbb"; structc_t sc; sc.a = 123456789; sc.b = 10000; print_structa_t(&sa); print_structb_t(&sb); print_structc_t(&sc); return 0; }
to identify each type of structure (when it's stored in file) can add "structtype" integer end of xm_struct_header() macro , use key switch(key){}
block deals each type correctly.
i wrote using ubuntu 15, can compile on platform gcc main.c -pedantic -std=gnu99 -o test
, run ./test
.
the output looks like:
--struct 'structa_t' int a: 123 char b: --end of struct --struct 'structb_t' custom_t a: .x: 1 .y: 2 .z: 3 char* b: bbbbb --end of struct --struct 'structc_t' long a: 123456789 short b: 10000 --end of struct
Comments
Post a Comment