Repository navigation
Expand file tree
/
Copy pathoutputfile.cpp
More file actions
64 lines (47 loc) · 1.72 KB
/
Copy pathoutputfile.cpp
File metadata and controls
64 lines (47 loc) · 1.72 KB
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
#include "outputfile.h"
#include <stdexcept>
#include <fstream>
OutputFile::OutputFile(fs::path path) : path(path) {
fs::remove(path);
}
void OutputFile::writeContent(std::vector<unsigned char> data) {
std::ofstream outputFile(path, std::ios::app);
if (!outputFile.is_open())
throw std::runtime_error("Could not open file: " + path.string());
for (unsigned char dataByte : data) {
outputFile.put(static_cast<char>(dataByte));
}
bytesWritten += data.size();
outputFile.close();
}
void OutputFile::replaceContent(std::vector<unsigned char> data, unsigned int address) {
std::fstream outputFile;
outputFile.open(path, std::fstream::in | std::fstream::out);
if (!outputFile.is_open())
throw std::runtime_error("Could not open file: " + path.string());
outputFile.seekp(address);
for (unsigned char dataByte : data) {
outputFile.put(static_cast<char>(dataByte));
}
outputFile.close();
}
unsigned int OutputFile::getBytesWritten() {
return bytesWritten;
}
// std::vector<unsigned char> InputFile::getContent(const size_t start, const size_t length) {
// std::vector<unsigned char> result;
// std::ifstream input_file(path);
// if (!input_file.is_open())
// throw std::runtime_error("Could not open file: " + path.string());
// char byte = 0;
// size_t bytesRead = 0;
// while (!input_file.eof() && bytesRead < length) {
// input_file.get(byte);
// result.push_back(static_cast<unsigned char>(byte));
// bytesRead++;
// }
// input_file.close();
// if (bytesRead != length)
// throw std::runtime_error("Could not read from file: " + path.string());
// return result;
// }