Website Summary Files Commits Branches @commit @diff @stats
path: root / BinaryFiles / SaveStructsToBinaryFile.cpp
diff options
context:
space:
mode:
1 files changed, 40 insertions, 0 deletions
-rw-r--r--BinaryFiles/SaveStructsToBinaryFile.cpp40
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