From e466c7b322a818d7cfe3204c0f2729c324ce8d6a Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Wed, 21 Dec 2022 19:42:45 +0100 Subject: [PATCH] added dump, makefile todo --- TODO.md | 1 + dump.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 dump.c diff --git a/TODO.md b/TODO.md index 9e08295..00fedd4 100644 --- a/TODO.md +++ b/TODO.md @@ -1,3 +1,4 @@ # TODO ### This file contains ideas for new small projects i would like to add to this repo. calculate big exponents, factorials +fix damn makefile, it compiles only abc.c into all the binaries diff --git a/dump.c b/dump.c new file mode 100644 index 0000000..d8cea6b --- /dev/null +++ b/dump.c @@ -0,0 +1,42 @@ +#include +#include +#include + +struct a { + int a; + char str[30]; +}; + +/* + * give an argument to keep the file /tmp/structdump + * like this: bin/dump aaa + */ + +int main(int argc, char** argv){ + struct a* mystruct = malloc(sizeof(struct a)); + if(mystruct == NULL){ + printf("malloc for 1st struct failed!\n"); + return 1; + } + mystruct->a = 12; + char s[30] = "Wer das liest ist doof"; + strcpy(mystruct->str, s); + FILE* fptr = fopen("/tmp/structdump", "wb"); + fwrite(mystruct, sizeof(struct a), 1, fptr); + fclose(fptr); + free(mystruct); + + fptr = fopen("/tmp/structdump", "rb"); + mystruct = malloc(sizeof(struct a)); + if(mystruct == NULL){ + printf("malloc for 2nd struct failed!\n"); + return 1; + } + fread(mystruct, sizeof(struct a), 1, fptr); + printf("mystruct->a: %d\nmystruct->str: \"%s\"\n", mystruct->a, mystruct->str); // SIGSEV + free(mystruct); + fclose(fptr); + if(argc<2) + remove("/tmp/structdump"); + return 0; +}