added dump, makefile todo

This commit is contained in:
Christoph J. Scherr 2022-12-21 19:42:45 +01:00
parent a03028e206
commit e466c7b322
2 changed files with 43 additions and 0 deletions

View File

@ -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

42
dump.c Normal file
View File

@ -0,0 +1,42 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}