From 0d9276b3ef084ef8504b32c4d8832be74b514018 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Wed, 20 Mar 2019 12:38:39 +0200 Subject: Add files via upload --- BinaryFiles/GetStructsFromBinaryFile.cpp | 47 ++++++++++++++++++++++ BinaryFiles/SaveStructsToBinaryFile(as array).cpp | 37 +++++++++++++++++ BinaryFiles/SaveStructsToBinaryFile.cpp | 40 ++++++++++++++++++ BinaryFiles/Structs.bat | Bin 0 -> 24 bytes BinaryFiles/StructsBAT (how does it look).png | Bin 0 -> 6050 bytes 5 files changed, 124 insertions(+) create mode 100644 BinaryFiles/GetStructsFromBinaryFile.cpp create mode 100644 BinaryFiles/SaveStructsToBinaryFile(as array).cpp create mode 100644 BinaryFiles/SaveStructsToBinaryFile.cpp create mode 100644 BinaryFiles/Structs.bat create mode 100644 BinaryFiles/StructsBAT (how does it look).png (limited to 'BinaryFiles') diff --git a/BinaryFiles/GetStructsFromBinaryFile.cpp b/BinaryFiles/GetStructsFromBinaryFile.cpp new file mode 100644 index 0000000..2d91189 --- /dev/null +++ b/BinaryFiles/GetStructsFromBinaryFile.cpp @@ -0,0 +1,47 @@ +#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/SaveStructsToBinaryFile(as array).cpp b/BinaryFiles/SaveStructsToBinaryFile(as array).cpp new file mode 100644 index 0000000..b8d5cfe --- /dev/null +++ b/BinaryFiles/SaveStructsToBinaryFile(as array).cpp @@ -0,0 +1,37 @@ +#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 new file mode 100644 index 0000000..5e03b44 --- /dev/null +++ b/BinaryFiles/SaveStructsToBinaryFile.cpp @@ -0,0 +1,40 @@ +#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"<