diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-06-22 20:01:14 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-06-22 20:01:14 +0200 |
| commit | 134f855b537b714eb98bc1e6e1e4790b4d6491a3 (patch) | |
| tree | c1cc9d813decd8a135a699a023977e1fb095da0c /c-structs/write.c | |
| parent | d9b6fe0bd815da591d9d16efb187ddcfb2aba109 (diff) | |
| download | probe-134f855b537b714eb98bc1e6e1e4790b4d6491a3.tar.gz | |
Added writing and reading structs from files
Diffstat (limited to 'c-structs/write.c')
| -rw-r--r-- | c-structs/write.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/c-structs/write.c b/c-structs/write.c new file mode 100644 index 0000000..e83531a --- /dev/null +++ b/c-structs/write.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <string.h> + +#include "struct.h" + +int main(void) { + printf("Write struct\n"); + + Character ch; + + strcpy(ch.name, "John Doe"); + ch.health = 30; + ch.damage = 5.9; + + FILE* file = fopen("character.dat", "wb"); + if (file == NULL) { + perror("Error opening file"); + return 1; + } + + fwrite(&ch, sizeof(Character), 1, file); + fclose(file); + + return 0; +} |
