#include <sstream>
#include <fstream>
#include <iostream>
int main(int argc, char *argv[]) {
if (argc != 3) {
std::cerr << "Usage: genheader input_file output_file\n";
return 1;
}
std::ifstream input_file(argv[1]);
std::ofstream output_file(argv[2]);
output_file << "#ifndef PRINTHEADER_H\n";
output_file << "#define PRINTHEADER_H\n";
output_file << "#include <iostream>\n\n";
output_file << "void printheader() {\n";
std::string line;
std::getline(input_file, line); // saltear primera linea
std::string end = "****************************************"
"***************************************/";
while (std::getline(input_file, line) && line != end) {
output_file << " std::cout << \"";
output_file << std::hex;
for (unsigned char c : line) {
output_file << "\\x" << static_cast<unsigned>(c);
}
output_file << std::dec;
output_file << "\\n\";\n";
}
output_file << "}\n";
output_file << "#endif\n";
}
./genheader main.cpp printheader.h
El resultado es un archivo parecido a este:
printheader.h
#ifndef PRINTHEADER_H
#define PRINTHEADER_H
#include <iostream>
void printheader() {
std::cout << "\x4e\x4f\x4d\x42\x52\x45\x3a\x20\x23\x4a\x75\x61\x6e\x23\n";
std::cout << "\x50\x52\x49\x4d\x45\x52\x20\x41\x50\x45\x4c\x4c\x49\x44\x4f\x3a\x20\x23\x46\x65\x72\x6e\x61\x6e\x64\x65\x7a\x23\n";
std::cout << "\x53\x45\x47\x55\x4e\x44\x4f\x20\x41\x50\x45\x4c\x4c\x49\x44\x4f\x3a\x20\x23\x47\x6f\x6e\x7a\x61\x6c\x65\x7a\x23\n";
std::cout << "\x44\x4e\x49\x3a\x20\x23\x31\x31\x31\x31\x31\x31\x31\x31\x23\n";
std::cout << "\x45\x4d\x41\x49\x4c\x3a\x20\x23\x6a\x61\x6e\x74\x6f\x6e\x69\x6f\x2e\x67\x6f\x6e\x7a\x61\x6c\x65\x7a\x40\x6d\x69\x6d\x61\x69\x6c\x2e\x63\x6f\x6d\x23\n";
}
#endif
Cada número en hexadecimal es un caracter de los datos. Lo hice de esta forma para no
tener que "escapar" los caracteres conflictivos como la comilla "
o
la barra invertida \
.