1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
//Program for writing and reading data safely to binary file in binary format.
//It used Little Endianness and reads/writes low byte first.
#include <iostream>
#include <fstream>
#include "MyDataTypes.h"
using namespace std;
//Declaring function prototypes for reading
u8 readu8(ifstream&);
u16 readu16(ifstream&);
u32 readu32(ifstream&);
u64 readu64(ifstream&);
//Declaring function prototypes for writing
void writeu8(ofstream&, u8);
void writeu16(ofstream&, u16);
void writeu32(ofstream&, u32);
void writeu64(ofstream&, u64);
//Declaring function prototype for reading string.
string& readstring(ifstream&);
//Declaring function prototype for writing string.
void writestring(ofstream&, string);
using namespace std;
class Human {
public:
Human(string n = "Default", u8 a = 26): name(n), age(a) {}
string name;
u8 age;
void write(ofstream& fileout) const;
void read(ifstream& filein);
protected:
private:
};
void Human::write(ofstream& fileout) const {
writestring(fileout, name);
writeu8(fileout, age);
}
void Human::read(ifstream& filein) {
name = readstring(filein);
age = readu8(filein);
}
int main() {
ifstream filein("testfile.bin", ios::in | ios::binary);
filein.close();
ofstream fileout("testfile.bin", ios::out | ios::binary);
fileout.close();
Human h("Tobias", 19);
h.write(fileout);
system("pause");
exit(EXIT_SUCCESS);
return 0;
}
//Defining functions for reading
u8 readu8(ifstream& filein) {
u8 byte = 0;
char val = 0;
filein.open("testfile.bin", ios::in | ios::binary);
if(!filein){
cout << "The file could not be opened by function "
<< "readu8." << endl;
exit(EXIT_FAILURE);
}
filein.read((char*) byte, 1);
filein.close();
val = val | byte;
return val;
}
u16 readu16(ifstream& filein) {
char bytes[2];
u16 val;
filein.open("testfile.bin", ios::in | ios::binary);
if(!filein) {
cout << "The file could not be opened by function "
<< "readu16." << endl;
exit(EXIT_FAILURE);
}
filein.read((char*)bytes, 2);
filein.close();
val = bytes[0] | (bytes[1] << 8);
return val;
}
u32 readu32(ifstream& filein) {
u32 val;
char bytes[4];
filein.open("testfile.bin", ios::in | ios::binary);
if(!filein) {
cout << "The file could not be opened by function "
<< "readu32." << endl;
exit(EXIT_FAILURE);
}
filein.read((char*) bytes, 4);
filein.close();
val = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24);
return val;
}
u64 readu64(ifstream& filein) {
u64 val;
char bytes[8];
filein.open("testfile.bin", ios::in | ios::binary);
if(!filein) {
cout << "The file could not be opened by function "
<< " readu64." << endl;
exit(EXIT_FAILURE);
}
filein.read((char*) bytes, 8);
filein.close();
val = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24)
| (static_cast<u64>(bytes[4])) << 32 | (static_cast<u64>(bytes[5])) << 40 | (static_cast<u64>(bytes[6])) << 48 | (static_cast<u64>(bytes[7])) << 56;
return val;
}
//Defining functions for writing binary data.
void writeu8(ofstream& fileout, u8 val) {
char byte;
byte = val;
fileout.open("testfile.bin", ios::out | ios::binary);
if(!fileout) {
cout << "THe file could not be opened by function "
<< "writeu8." << endl;
exit(EXIT_FAILURE);
}
fileout.write((char*) byte, 1);
fileout.close();
}
void writeu16(ofstream& fileout, u16 val) {
char bytes[2];
bytes[0] = (val & 0xFF);
bytes[1] = (val & 0xFF00) >> 8;
fileout.open("testfile.bin", ios::out | ios::binary);
if(!fileout) {
cout << "The file could not be opened by function "
<< "writeu16." << endl;
exit(EXIT_FAILURE);
}
fileout.write((char*) bytes, 2);
fileout.close();
}
void writeu32(ofstream& fileout, u32 val) {
char bytes[4];
bytes[0] = (val & 0x000000FF);
bytes[1] = (val & 0x0000FF00) >> 8;
bytes[2] = (val & 0x00FF0000) >> 16;
bytes[3] = (val & 0xFF000000) >> 24;
fileout.open("testfile.bin", ios::out | ios::binary);
if(!fileout) {
cout << "The file could not be opened by function "
<< "writeu32." << endl;
exit(EXIT_FAILURE);
}
fileout.write((char*) bytes, 4);
fileout.close();
}
void writeu64(ofstream& fileout, u64 val) {
char bytes[8];
fileout.open("testfile.bin", ios::out | ios::binary);
bytes[0] = (val & 0xFF);
bytes[1] = (val & 0xFF00) >> 8;
bytes[2] = static_cast<u8> (val & 0x0000000000FF0000) >> 16;
bytes[3] = static_cast<u8> (val & 0x00000000FF000000) >> 24;
bytes[4] = static_cast<u8> (static_cast<u64> (val & 0x0000000FF0000000) >> 32);
bytes[5] = static_cast<u8> (static_cast<u64> (val & 0x0000FF0000000000) >> 40);
bytes[6] = static_cast<u8> (static_cast<u64> (val & 0x00FF000000000000) >> 48);
bytes[7] = static_cast<u8> (static_cast<u64> (val & 0xFF00000000000000) >> 56);
if(!fileout) {
cout << "The file could not be opened by function "
<< "writeu64." << endl;
exit(EXIT_FAILURE);
}
fileout.write((char*) bytes, 8);
fileout.close();
}
//Defining function for writing strings.
void writestring(ofstream& fileout, string str) {
u32 len = str.length();
fileout.open("testfile.bin", ios::out | ios::binary);
if(!fileout) {
cout << "The function could not be opened by function "
<< "writestring." << endl;
exit(EXIT_FAILURE);
}
writeu32(fileout, len);
fileout.write(str.c_str(), len);
fileout.close();
}
//Defining function for reading string
string& readstring(ifstream& filein) {
u32 len = readu32(filein);
char* buffer = new char[len];
filein.open("testfile.bin", ios::in | ios::binary);
if(!filein) {
cout << "The file could not be opened by function "
<< "readstring." << endl;
exit(EXIT_FAILURE);
}
filein.read(buffer, len);
filein.close();
string str(buffer, len);
delete[] buffer;
return str;
}
|