-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred2bin.cpp
84 lines (69 loc) · 1.82 KB
/
red2bin.cpp
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
/*
Name: Red2Bin (Readable To Binary)
Author: William Kimberley (Kodeumeister)
Date Modified: 2019-12-07
Date Created: 2019-12-07
*/
// C++ Libs
#include <iostream>
#include <fstream>
#include <string>
// C Libs
#include <cstdlib>
// Author's Libs
// NONE!!!
// I usually don't use the "using namespace" statement but
// I thought I should since there'll be no namespace
// clashes and it makes the code easier to read.
using namespace std;
int main(int argc, char* argv[]) {
if (argc < 2) {
cout << "\nUsage: red2bin <file>\n";
} else if (argc > 2) {
cout << "\nToo many arguments.\n";
} else {
// Initialising objects;
fstream in, out;
// Setting exceptions.
in.exceptions(fstream::failbit);
out.exceptions(fstream::failbit);
// Settting up output stuff.
string output;
size_t found = string::npos;
char byte = 0x00;
size_t pos = 0x00;
// Converting filename to string.
string filename(argv[1]);
try {
// Opening files.
in.open(filename, ios::in);
// Creating "binary" file.
out.open(filename + ".bin", ios::binary | ios::out);
// Converting text to binary.
do {
in >> output;
found = output.find(";");
output.erase(4,4);
byte = stoi(output, &pos, 16);
out.write(&byte, sizeof(byte));
} while (found == string::npos);
cout << "\nSuccessfully reconstructed binary.\n";
} catch (const fstream::failure& e) {
// Showing error.
cerr << "\nFailed to read and write to files.\n";
// Making sure files get closed.
if (in.is_open()) {
in.close();
}
if (out.is_open()) {
out.close();
}
// Returning that the program has failed.
return EXIT_FAILURE;
}
// Closing files.
in.close();
out.close();
}
return EXIT_SUCCESS;
}