From 775b4bd643cc8d6c023434a09efb8a03682afc7c Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Tue, 10 Oct 2023 11:24:31 +0100 Subject: [PATCH] C19 --- C19/ex1.c | 20 ++++++++++++++++++++ C19/ex2.c | 25 +++++++++++++++++++++++++ C19/ex3.c | 27 +++++++++++++++++++++++++++ C19/ex4.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 C19/ex1.c create mode 100644 C19/ex2.c create mode 100644 C19/ex3.c create mode 100644 C19/ex4.c diff --git a/C19/ex1.c b/C19/ex1.c new file mode 100644 index 0000000..f6dcd03 --- /dev/null +++ b/C19/ex1.c @@ -0,0 +1,20 @@ +#include + + +struct Person { + int age; + char forename[30]; + char surname[50]; +}; + + +int main() { + struct Person p; + + printf("Enter age: "); scanf("%d", &p.age); + printf("Enter forename: "); scanf("%29s", p.forename); + printf("Enter surname: "); scanf("%49s", p.surname); + + printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname); + return 0; +} diff --git a/C19/ex2.c b/C19/ex2.c new file mode 100644 index 0000000..51e153c --- /dev/null +++ b/C19/ex2.c @@ -0,0 +1,25 @@ +#include + + +struct Person { + int age; + char forename[30]; + char surname[50]; +}; + + +void print_person(struct Person p) { + printf("p.age=%d p.forename=%s p.surname=%s\n", p.age, p.forename, p.surname); +} + + +int main() { + struct Person p; + + printf("Enter age: "); scanf("%d", &p.age); + printf("Enter forename: "); scanf("%29s", p.forename); + printf("Enter surname: "); scanf("%49s", p.surname); + + print_person(p); + return 0; +} diff --git a/C19/ex3.c b/C19/ex3.c new file mode 100644 index 0000000..96dc041 --- /dev/null +++ b/C19/ex3.c @@ -0,0 +1,27 @@ +#include + + +struct Person { + int age; + int year_of_birth; + char forename[30]; + char surname[50]; +}; + + +void print_person(struct Person p) { + printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname); +} + + +int main() { + struct Person p; + + printf("Enter age: "); scanf("%d", &p.age); + printf("Enter birth year: "); scanf("%d", &p.year_of_birth); + printf("Enter forename: "); scanf("%29s", p.forename); + printf("Enter surname: "); scanf("%49s", p.surname); + + print_person(p); + return 0; +} diff --git a/C19/ex4.c b/C19/ex4.c new file mode 100644 index 0000000..76b47cf --- /dev/null +++ b/C19/ex4.c @@ -0,0 +1,50 @@ +#include + + +struct Person { + int age; + int year_of_birth; + char forename[30]; + char surname[50]; +}; + + +void print_person(struct Person p) { + printf("p.age=%d p.year_of_birth=%d p.forename=%s p.surname=%s\n", p.age, p.year_of_birth, p.forename, p.surname); +} + + +int main() { + struct Person p; + FILE *fp; + char choice; + char filename[4097]; + + printf("Read or write a file? (r/w) "); scanf("%c", &choice); + + if (choice == 'w') { + printf("Enter age: "); scanf("%d", &p.age); + printf("Enter birth year: "); scanf("%d", &p.year_of_birth); + printf("Enter forename: "); scanf("%29s", p.forename); + printf("Enter surname: "); scanf("%49s", p.surname); + + printf("Enter filename to save to: "); scanf("%4096s", filename); + fp = fopen(filename, "wb"); + if (fp == NULL) { + printf("Failed to open file for writing\n"); + return 1; + } + fwrite(&p, 1, sizeof(p), fp); + } else { + printf("Enter file to read from: "); scanf("%4096s", filename); + fp = fopen(filename, "rb"); + if (fp == NULL) { + printf("Failed to open file for reading\n"); + return 1; + } + fread(&p, 1, sizeof(p), fp); + } + + print_person(p); + return 0; +}