Imprime el fichero binario por pantalla

This commit is contained in:
2023-04-20 16:16:13 +02:00
commit 39c9777455
3 changed files with 72 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
#include <vector>
#include <string>
#include <iostream> // std::cout
#include <fstream> // ifstream, ofstream
#include <libgen.h> // basename()
#include <iterator>
using namespace std;
ifstream inputFile;
string filePath = "";
vector<unsigned char> buffer;
void checkParameters(int argc, char *argv[])
{
if (argc != 2)
{
cout << "Uso: " << basename(argv[0]) << " FILE" << endl;
exit(EXIT_FAILURE);
}
else
filePath = argv[1];
}
void loadFile(string s)
{
inputFile.open(s, ios::binary);
if (!inputFile.good())
{
cout << "No se puede abrir el archivo: " << s << endl;
exit(EXIT_FAILURE);
}
// Copia el stream al buffer
vector<unsigned char> temp(std::istreambuf_iterator<char>(inputFile), {});
buffer = temp;
}
void printBuffer()
{
int count = 0;
for (auto i : buffer)
{
cout << static_cast<unsigned>(i) << " ";
count++;
if (count % 16 == 0)
cout << static_cast<unsigned>(i) << endl;
}
}
int main(int argc, char *argv[])
{
checkParameters(argc, argv);
loadFile(filePath);
printBuffer();
cout << "Fin del programa" << endl;
return 0;
}