Website Summary Files Commits Branches @commit @diff @stats
path: root / BinaryFiles
diff options
context:
space:
mode:
Commit0d9276b3ef084ef8504b32c4d8832be74b514018 (patch)
Author
 Angeld55 <achi_d@abv.bg>
2019-03-20 12:38:39 +0200
Committer
 GitHub <noreply@github.com>
2019-03-20 12:38:39 +0200
Filesa06da25ff5c55c366f0db5a5ab38a37f881b0e41 /BinaryFiles
Object-oriented_programming_FMI-0d9276b3ef084ef8504b32c4d8832be74b514018.tar
Object-oriented_programming_FMI-0d9276b3ef084ef8504b32c4d8832be74b514018.tar.gz
Object-oriented_programming_FMI-0d9276b3ef084ef8504b32c4d8832be74b514018.zip
Parentaeb6c5d8e667e2474374b2db4a79a7e5aa0cbd8a (diff)
Add files via upload
5 files changed, 124 insertions, 0 deletions
-rw-r--r--BinaryFiles/GetStructsFromBinaryFile.cpp47
-rw-r--r--BinaryFiles/SaveStructsToBinaryFile(as array).cpp37
-rw-r--r--BinaryFiles/SaveStructsToBinaryFile.cpp40
-rw-r--r--BinaryFiles/Structs.batbin0 -> 24 bytes
-rw-r--r--BinaryFiles/StructsBAT (how does it look).pngbin0 -> 6050 bytes
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 <iostream>
+#include <fstream>
+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 <iostream>
+#include <fstream>
+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 <iostream>
+#include <fstream>
+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"<<endl;
+ return 1;
+ }
+
+ //запазваме 3-те обекта във файла
+ outF.write((const char*)&obj1, sizeof(test));
+ outF.write((const char*)&obj2, sizeof(test));
+ outF.write((const char*)&obj3, sizeof(test));
+
+ 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/Structs.bat b/BinaryFiles/Structs.bat
new file mode 100644
index 0000000..37573c8
--- /dev/null
+++ b/BinaryFiles/Structs.bat
Binary files differ
diff --git a/BinaryFiles/StructsBAT (how does it look).png b/BinaryFiles/StructsBAT (how does it look).png
new file mode 100644
index 0000000..5e9e5b3
--- /dev/null
+++ b/BinaryFiles/StructsBAT (how does it look).png
Binary files differ