From 112a0d344da9e66d20ee9ab5d488e59c5d52e6ad Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Thu, 9 Apr 2020 18:09:42 +0300 Subject: added new examples --- BinaryFiles/BinaryFiles_Cheatsheet.pdf | Bin 61351 -> 0 bytes BinaryFiles/GetStructsFromBinaryFile.cpp | 47 ---------------------- BinaryFiles/README.md | 5 --- BinaryFiles/SaveAndReadStudents_BinaryFile.cpp | 47 ---------------------- BinaryFiles/SaveStructsToBinaryFile(as array).cpp | 37 ----------------- BinaryFiles/SaveStructsToBinaryFile.cpp | 40 ------------------ BinaryFiles/Structs.bat | Bin 24 -> 0 bytes BinaryFiles/StructsBAT (how does it look).png | Bin 6050 -> 0 bytes 8 files changed, 176 deletions(-) delete mode 100644 BinaryFiles/BinaryFiles_Cheatsheet.pdf delete mode 100644 BinaryFiles/GetStructsFromBinaryFile.cpp delete mode 100644 BinaryFiles/README.md delete mode 100644 BinaryFiles/SaveAndReadStudents_BinaryFile.cpp delete mode 100644 BinaryFiles/SaveStructsToBinaryFile(as array).cpp delete mode 100644 BinaryFiles/SaveStructsToBinaryFile.cpp delete mode 100644 BinaryFiles/Structs.bat delete mode 100644 BinaryFiles/StructsBAT (how does it look).png (limited to 'BinaryFiles') diff --git a/BinaryFiles/BinaryFiles_Cheatsheet.pdf b/BinaryFiles/BinaryFiles_Cheatsheet.pdf deleted file mode 100644 index 397177c..0000000 Binary files a/BinaryFiles/BinaryFiles_Cheatsheet.pdf and /dev/null differ diff --git a/BinaryFiles/GetStructsFromBinaryFile.cpp b/BinaryFiles/GetStructsFromBinaryFile.cpp deleted file mode 100644 index 2d91189..0000000 --- a/BinaryFiles/GetStructsFromBinaryFile.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include -using namespace std; - -struct test -{ - char ch; - int num; -}; - -int GetFileSize(ifstream& ifs) -{ - ifs.seekg(0, std::ios::end); //отиваме в края на файла - int size = ifs.tellg(); //запазваме коя е текущата позиция(тя всъщност е и размерът на файла) - ifs.seekg(0, std::ios::beg); //връщаме се в началото на файла - return size; -//тази функция е хубаво да я извикваме преди да сме чели каквото и да е от файла, понеже ще се върнем в началото и ще загубим информация до къде сме стигнали с четенето -} - - -int main() -{ - std::ifstream inF("Structs.bat", std::ios::binary); - - if (!inF.is_open()) - return false; - - int fileSize = GetFileSize(inF); // взимаме големината на файла - int objectCount = fileSize / sizeof(test); // намираме броя на обектите записани във файла - - test* arr = new test[objectCount]; - - inF.read((char*)arr, fileSize); //директно ги четем в масива - - if (!inF) - { - cout << "Error occurred while reading from the file"; - return 1; - } - inF.close(); - - for (int i = 0; i < objectCount; i++) - cout << arr[i].ch << " " << arr[i].num << endl; - delete[] arr; - - return 0; -} \ No newline at end of file diff --git a/BinaryFiles/README.md b/BinaryFiles/README.md deleted file mode 100644 index f740e9f..0000000 --- a/BinaryFiles/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# How does the binary file look like? -The file after running **SaveStructsToBinaryFile.cpp** or **SaveStructsToBinaryFile(as array).cpp** - -![Image of the binary file after running the code](https://lh3.googleusercontent.com/NcLcTEemBj6dMkOFFjUPo2jyR_7vdrUkn0dHqx8TX1CYmtW-XZzSaDwAxscJz0m3EnniPL68ag "Binary file") - diff --git a/BinaryFiles/SaveAndReadStudents_BinaryFile.cpp b/BinaryFiles/SaveAndReadStudents_BinaryFile.cpp deleted file mode 100644 index 0f600aa..0000000 --- a/BinaryFiles/SaveAndReadStudents_BinaryFile.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include -#include - -using namespace std; -void SaveStudent(const char* name, int fn, ofstream& f) -{ - f.write((const char*)&fn, sizeof(int)); //sizeof(int) is 4 - int strLen = strlen(name); - f.write((const char*)&strLen,sizeof(int)); - f.write(name, strLen); -} -void ReadStudent(ifstream& f) -{ - int fn; - f.read((char*)&fn, sizeof(int)); - - int nameLen; - f.read((char*)&nameLen, sizeof(int)); - - char* name = new char[nameLen + 1]; - f.read(name, nameLen); - name[nameLen] = '\0'; - - cout << "Name: " << name << " Fn: " << fn << endl; - delete[] name; - -} - - - -int main() -{ - //ofstream f; - // f.open("students.bat"); - // SaveStudent("Ivan",123,f); - // SaveStudent("Sasho",555,f); - // f.close(); - - //ifstream f2; - //f2.open("students.bat"); - //ReadStudent(f2); - //ReadStudent(f2); - //f2.close(); - - - -} diff --git a/BinaryFiles/SaveStructsToBinaryFile(as array).cpp b/BinaryFiles/SaveStructsToBinaryFile(as array).cpp deleted file mode 100644 index b8d5cfe..0000000 --- a/BinaryFiles/SaveStructsToBinaryFile(as array).cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -using namespace std; - -struct test -{ - char ch; - int num; -}; - -int main() -{ - - test arr[] = { { 'a', 4 }, { 'b', 5 }, { 'c', 6 } }; - - ofstream outF("Structs.bat", ios::binary); - - if (!outF.is_open()) - { - cout << "Error while opening file" << endl; - return 1; - } - - outF.write((const char*)&arr, sizeof(arr)); - //можем директно да запазим масива във файла. Резултатът ще същия като от SaveStructToBinaryFile.cpp - - - if (!outF.good()) - { - cout << "Error occurred while writing the file"; - return 1; - } - - outF.close(); - - return 0; -} \ No newline at end of file diff --git a/BinaryFiles/SaveStructsToBinaryFile.cpp b/BinaryFiles/SaveStructsToBinaryFile.cpp deleted file mode 100644 index 5e03b44..0000000 --- a/BinaryFiles/SaveStructsToBinaryFile.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -using namespace std; - -struct test -{ - char ch; - int num; -}; - -int main() -{ - - test obj1{ 'a', 4 }; - test obj2{ 'b', 5 }; - test obj3{ 'c', 6 }; - - ofstream outF("Structs.bat", ios::binary); - - if (!outF.is_open()) - { - cout << "Error while opening file"<