diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-06-22 19:57:42 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2024-06-22 19:57:42 +0200 |
| commit | 2088ea284e15ec42a475ea8a0841e2579cd9c937 (patch) | |
| tree | 13f2ef2434704df0af85a81f3fcc050c98d897eb /content/notes | |
| parent | 5269ad50ddb0b641aa96dac7c8f49b156646d4c4 (diff) | |
| download | mitjafelicijan.com-2088ea284e15ec42a475ea8a0841e2579cd9c937.tar.gz | |
New note: Read, write structs
Diffstat (limited to 'content/notes')
| -rw-r--r-- | content/notes/2024-06-22-write-and-read-structs-to-files-in-c.md | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/content/notes/2024-06-22-write-and-read-structs-to-files-in-c.md b/content/notes/2024-06-22-write-and-read-structs-to-files-in-c.md new file mode 100644 index 0000000..d5508e8 --- /dev/null +++ b/content/notes/2024-06-22-write-and-read-structs-to-files-in-c.md | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | --- | ||
| 2 | title: "Write and read structs to/from files in C" | ||
| 3 | url: write-and-read-structs-to-files-in-c.html | ||
| 4 | date: 2024-06-22T16:13:13+02:00 | ||
| 5 | type: note | ||
| 6 | draft: false | ||
| 7 | tags: [c] | ||
| 8 | --- | ||
| 9 | |||
| 10 | First let's define a shared header file for the struct definition. | ||
| 11 | |||
| 12 | ```c | ||
| 13 | // struct.h | ||
| 14 | typedef struct { | ||
| 15 | char name[50]; | ||
| 16 | int health; | ||
| 17 | float damage; | ||
| 18 | } Character; | ||
| 19 | ``` | ||
| 20 | |||
| 21 | Now lets write it to a `character.dat` file. | ||
| 22 | |||
| 23 | ```c | ||
| 24 | // write.c | ||
| 25 | #include <stdio.h> | ||
| 26 | #include <string.h> | ||
| 27 | |||
| 28 | #include "struct.h" | ||
| 29 | |||
| 30 | int main(void) { | ||
| 31 | printf("Write struct\n"); | ||
| 32 | |||
| 33 | Character ch; | ||
| 34 | |||
| 35 | strcpy(ch.name, "John Doe"); | ||
| 36 | ch.health = 30; | ||
| 37 | ch.damage = 5.9; | ||
| 38 | |||
| 39 | FILE* file = fopen("character.dat", "wb"); | ||
| 40 | if (file == NULL) { | ||
| 41 | perror("Error opening file"); | ||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | fwrite(&ch, sizeof(Character), 1, file); | ||
| 46 | fclose(file); | ||
| 47 | |||
| 48 | return 0; | ||
| 49 | } | ||
| 50 | ``` | ||
| 51 | |||
| 52 | And reading and serializing back to a struct. | ||
| 53 | |||
| 54 | ```c | ||
| 55 | // read.c | ||
| 56 | #include <stdio.h> | ||
| 57 | |||
| 58 | #include "struct.h" | ||
| 59 | |||
| 60 | int main(void) { | ||
| 61 | printf("Read struct\n"); | ||
| 62 | |||
| 63 | Character ch; | ||
| 64 | |||
| 65 | FILE* file = fopen("character.dat", "rb"); | ||
| 66 | if (file == NULL) { | ||
| 67 | perror("Error opening file"); | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | fread(&ch, sizeof(Character), 1, file); | ||
| 72 | fclose(file); | ||
| 73 | |||
| 74 | printf("Name: %s\n", ch.name); | ||
| 75 | printf("Health: %d\n", ch.health); | ||
| 76 | printf("Damage: %.1f\n", ch.damage); | ||
| 77 | |||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | ``` | ||
| 81 | |||
