Coś takiego przyszło mi do głowy:
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
void print(char* buffer, int size) {
for (int i = 0; i < size; ++i)
cout << hex << setw(4) << static_cast<int>(buffer[i]) << " ";
for (int i = 0; i < size; ++i)
if(buffer[i] != '\n') cout << buffer[i];
cout << endl;
}
int countBytes(fstream& file) {
file.seekg(0, file.end);
int bytes = static_cast<int>(file.tellg());
file.seekg(0, file.beg);
return bytes;
}
void hexRead(const char* path) {
fstream file(path, ios::in | ios::binary);
if (file.is_open()) {
int length = 8;
char* buffer = new char[length];
int steps = countBytes(file) / length;
int last = countBytes(file) % length;
for (int i = 0; i < steps; ++i) {
file.read((char*)buffer, length * sizeof(char));
print(buffer, length);
}
file.read((char*)buffer, last * sizeof(char));
print(buffer, last);
delete[] buffer;
file.close();
}
}
int main() {
hexRead("test.txt");
return 0;
}
Przykładowy wynik:
47 72 7a 65 73 69 65 6b Grzesiek
20 54 6f 6d 65 6b d a Tomek
52 79 73 69 65 6b d a Rysiek
4d 61 63 69 65 6b 54 6f MaciekTo
6d 65 6b mek