Website Summary Files Commits Branches @commit @diff @stats
path: root / BinaryFiles / SaveStructsToBinaryFile(as array).cpp
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/SaveStructsToBinaryFile(as array).cpp
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
1 files changed, 37 insertions, 0 deletions
-rw-r--r--BinaryFiles/SaveStructsToBinaryFile(as array).cpp37
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