Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 03 / array_of_students_write.cpp
diff options
context:
space:
mode:
Commitce654711db90ee4121f3df12e373f2362f9674ad (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2022-03-15 19:26:57 +0200
Committer
 GitHub <noreply@github.com>
2022-03-15 19:26:57 +0200
Files4dffdcf55ebc683870c5ea4ccff2f45656eb22f4 /Sem. 03/array_of_students_write.cpp
Object-oriented_programming_FMI-ce654711db90ee4121f3df12e373f2362f9674ad.tar
Object-oriented_programming_FMI-ce654711db90ee4121f3df12e373f2362f9674ad.tar.gz
Object-oriented_programming_FMI-ce654711db90ee4121f3df12e373f2362f9674ad.zip
Parent22af9f54ecfcf622dd7c458394c6749594f7a6ce (diff)
Add files via upload
1 files changed, 38 insertions, 0 deletions
-rw-r--r--Sem. 03/array_of_students_write.cpp38
diff --git a/Sem. 03/array_of_students_write.cpp b/Sem. 03/array_of_students_write.cpp
new file mode 100644
index 0000000..3802bf8
--- /dev/null
+++ b/Sem. 03/array_of_students_write.cpp
@@ -0,0 +1,38 @@
+#include <iostream>
+#include <fstream>
+#include <cstring>
+using namespace std;
+
+struct Student
+{
+ char name[30];
+ int age;
+ int fn;
+};
+
+void initStudent(Student& st, const char* name, int age, int fn)
+{
+ strcpy(st.name, name);
+ st.age = age;
+ st.fn = fn;
+}
+
+void saveToFile(const Student* students, size_t count, ofstream& file)
+{
+ file.write((const char*)students, count * sizeof(Student));
+}
+
+
+int main()
+{
+ Student* arr = new Student[4];
+ initStudent(arr[0], "ivan", 44, 1234);
+ initStudent(arr[1], "petur", 12, 765);
+ initStudent(arr[2], "alex", 15, 44);
+ initStudent(arr[3], "katerina", 19, 12134);
+
+ ofstream file("students.dat", ios::binary);
+ saveToFile(arr, 4, file);
+
+ delete[] arr;
+} \ No newline at end of file