From 83698125ab430c523303e1fefd16451fd4568282 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Wed, 9 Mar 2022 15:56:33 +0200 Subject: Rename students_to_binary.cpp to students_dynamicName_to_binary.cpp --- Sem. 03/students_dynamicName_to_binary.cpp | 78 ++++++++++++++++++++++++++++++ Sem. 03/students_to_binary.cpp | 78 ------------------------------ 2 files changed, 78 insertions(+), 78 deletions(-) create mode 100644 Sem. 03/students_dynamicName_to_binary.cpp delete mode 100644 Sem. 03/students_to_binary.cpp diff --git a/Sem. 03/students_dynamicName_to_binary.cpp b/Sem. 03/students_dynamicName_to_binary.cpp new file mode 100644 index 0000000..f2f20dd --- /dev/null +++ b/Sem. 03/students_dynamicName_to_binary.cpp @@ -0,0 +1,78 @@ +#include +#include +#pragma warning(disable:4996) +using namespace std; + + +struct Student +{ + char* name; + int age; + int fn; +}; + +void saveStudent(ofstream& file, const Student& st) +{ + size_t nameLen = strlen(st.name) + 1; + file.write((const char*)&nameLen, sizeof(nameLen)); + file.write(st.name, nameLen); + file.write((const char*)&st.age, sizeof(st.age)); + file.write((const char*)&st.fn, sizeof(st.fn)); +} + +void loadStudent(ifstream& file, Student& st) +{ + size_t nameLen; + file.read((char*)&nameLen, sizeof(nameLen)); + + st.name = new char[nameLen]; + file.read(st.name, nameLen); + + file.read((char*)&st.age, sizeof(st.age)); + file.read((char*)&st.fn, sizeof(st.fn)); + +} + +int main() +{ + + { //writing students to binary file + + Student s1; + s1.name = new char[5]; + strcpy(s1.name, "ivan"); + s1.fn = 9999; + s1.age = 30; + + + Student s2; + s2.name = new char[6]; + strcpy(s2.name, "angel"); + s2.fn = 5555; + s2.age = 50; + + ofstream file("Students.dat", ios::binary); + saveStudent(file, s1); + saveStudent(file, s2); + + delete[] s1.name, s2.name; + } + + { + ifstream file("Students.dat", ios::binary); + Student s1, s2; + + loadStudent(file, s1); + loadStudent(file, s2); + std::cout << s1.name << " " << s1.age << " " << s1.fn << std::endl; + std::cout << s2.name << " " << s2.age << " " << s2.fn << std::endl; + + delete[] s1.name, s2.name; + } + + + + + + +} diff --git a/Sem. 03/students_to_binary.cpp b/Sem. 03/students_to_binary.cpp deleted file mode 100644 index f2f20dd..0000000 --- a/Sem. 03/students_to_binary.cpp +++ /dev/null @@ -1,78 +0,0 @@ -#include -#include -#pragma warning(disable:4996) -using namespace std; - - -struct Student -{ - char* name; - int age; - int fn; -}; - -void saveStudent(ofstream& file, const Student& st) -{ - size_t nameLen = strlen(st.name) + 1; - file.write((const char*)&nameLen, sizeof(nameLen)); - file.write(st.name, nameLen); - file.write((const char*)&st.age, sizeof(st.age)); - file.write((const char*)&st.fn, sizeof(st.fn)); -} - -void loadStudent(ifstream& file, Student& st) -{ - size_t nameLen; - file.read((char*)&nameLen, sizeof(nameLen)); - - st.name = new char[nameLen]; - file.read(st.name, nameLen); - - file.read((char*)&st.age, sizeof(st.age)); - file.read((char*)&st.fn, sizeof(st.fn)); - -} - -int main() -{ - - { //writing students to binary file - - Student s1; - s1.name = new char[5]; - strcpy(s1.name, "ivan"); - s1.fn = 9999; - s1.age = 30; - - - Student s2; - s2.name = new char[6]; - strcpy(s2.name, "angel"); - s2.fn = 5555; - s2.age = 50; - - ofstream file("Students.dat", ios::binary); - saveStudent(file, s1); - saveStudent(file, s2); - - delete[] s1.name, s2.name; - } - - { - ifstream file("Students.dat", ios::binary); - Student s1, s2; - - loadStudent(file, s1); - loadStudent(file, s2); - std::cout << s1.name << " " << s1.age << " " << s1.fn << std::endl; - std::cout << s2.name << " " << s2.age << " " << s2.fn << std::endl; - - delete[] s1.name, s2.name; - } - - - - - - -} -- cgit v1.3.1-23-gd82d