Website Summary Files Commits Branches @commit @diff @stats
path: root / k_1_SI / task2_write.cpp
diff options
context:
space:
mode:
Commit5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf (patch)
Author
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Committer
 angeld55 <angel_dimitriev@abv.bg>
2022-05-10 20:06:53 +0300
Files52f913ec9532fd07c8f679ccf1e68876d1fc9ff2 /k_1_SI/task2_write.cpp
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.tar.gz
Object-oriented_programming_FMI-5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf.zip
Parent459d3ac280aafdba703930455f3200b6e7b0f31b (diff)
sem 11
1 files changed, 0 insertions, 84 deletions
-rw-r--r--k_1_SI/task2_write.cpp84
diff --git a/k_1_SI/task2_write.cpp b/k_1_SI/task2_write.cpp
deleted file mode 100644
index f320557..0000000
--- a/k_1_SI/task2_write.cpp
+++ /dev/null
@@ -1,84 +0,0 @@
-#include <iostream>
-#include <fstream>
-#include <cstring>
-using namespace std;
-
-const size_t MAX_NAME_SIZE = 22;
-struct Student
-{
- //gender should be BEFORE fn so the struct would be 28 bytes.
- char name[MAX_NAME_SIZE + 1];
- bool isMale;
- size_t fn;
-};
-
-bool isLower(char ch)
-{
- return ch >= 'a' && ch <= 'z';
-}
-
-bool areAllLowerCase(const char* str)
-{
- while(*str)
- {
- if(!isLower(*str))
- return false;
- str++;
- }
- return true;
-}
-
-bool isUpper(char ch)
-{
- return ch >= 'A' && ch <= 'Z';
-}
-
-bool isNameValid(const char* name)
-{
- size_t len = strlen(name);
- if(len == 0 || len > MAX_NAME_SIZE)
- return false;
- return isUpper(*name) && areAllLowerCase(name + 1);
-}
-
-void initStudent(Student& st, const char* name, size_t fn, bool isMale)
-{
- if(!isNameValid(name))
- strcpy(st.name, "Unknown");
- else
- strcpy(st.name, name);
-
- st.isMale = isMale;
- st.fn = fn;
-}
-
-void saveToFile(const Student* students, size_t count, ofstream& file)
-{
- file.write((const char*)students, count * sizeof(Student));
-}
-
-
-int main()
-{
- const size_t studentsCount = 4;
- Student* arr = new Student[studentsCount];
- initStudent(arr[0], "Ivan", 1, true);
- initStudent(arr[1], "Petur", 2, true);
- initStudent(arr[2], "Alex", 3, true);
- initStudent(arr[3], "Katerina", 4, false);
-
- ofstream file("students.dat", ios::binary);
-
- if(!file.is_open())
- {
- std::cout << "Error while opening the file!" << std::endl;
- delete[] arr; //!
- return -1;
- }
-
- saveToFile(arr, studentsCount, file);
-
- delete[] arr;
-
- return 0;
-}