From 5dda3a1f40e4e8f621b20f374c02b6d1e241e7cf Mon Sep 17 00:00:00 2001 From: angeld55 Date: Tue, 10 May 2022 20:06:53 +0300 Subject: sem 11 --- k_1_SI/task2_read.cpp | 55 --------------------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 k_1_SI/task2_read.cpp (limited to 'k_1_SI/task2_read.cpp') diff --git a/k_1_SI/task2_read.cpp b/k_1_SI/task2_read.cpp deleted file mode 100644 index e6a8312..0000000 --- a/k_1_SI/task2_read.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include -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; -}; - - -size_t getFileSize(ifstream& f) -{ - size_t currentPos = f.tellg(); - f.seekg(0, ios::end); - size_t size = f.tellg(); - - f.seekg(currentPos); - return size; -} - -void readFromFile(Student*& ptr, size_t& studentsCount, ifstream& f) -{ - size_t sizeOfFile = getFileSize(f); - studentsCount = sizeOfFile / sizeof(Student); - ptr = new Student[studentsCount]; - f.read((char*)ptr, sizeOfFile); -} - - -int main() -{ - Student* arr; - size_t count; - ifstream file("students.dat"); - - if(!file.is_open()) - { - std::cout << "Error while opening the file!" << std::endl; - return -1; - } - - readFromFile(arr, count, file); - - for (int i = 0; i < count; i++) - std::cout << "Name: " << arr[i].name << ", fn: " << arr[i].fn << ", is male: " << arr[i].isMale << std::endl; - - delete[] arr; - - return 0; -} -- cgit v1.3.1-23-gd82d