diff options
1 files changed, 0 insertions, 84 deletions
| -rw-r--r-- | k_1_SI/task2_write.cpp | 84 |
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; -} |
