Hola andres_89.
Si estás usando C++ Builder de modo visual, en lugar de usar la librería estándar
ifstream, te conviene aprovechar la
VCL usando
TFileStream,
aquí hay algunos ejemplos:
File Processing, buscando seguramente encontraras mas material.
No sé si interpreté lo que queres hacer, pero te pongo un ejemplo acorde a lo que entendí:
Código:
String file_hex(const String aFileName, const int LoPos, int HiPos)
{
TFileStream *fs = new TFileStream(aFileName, fmOpenRead); // abrir p/lectura
unsigned char *buff = new unsigned char[fs->Size]; // tamaño buffer
String str = "";
try {
fs->Read(&buff, fs->Size-1);
for (int i = LoPos; i <= HiPos; i++)
str += IntToHex(buff[i], 2) + ' ';
}
__finally {
delete[] buff;
delete fs;
}
return str.SetLength(str.Length()-1); // quitar el último ' '
}
Ejemplo de uso, obtener en un
TEdit los primeros 20 valores del archivo notepad.exe en formato hexadecimal:
Código:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Edit1->Text = file_hex("C:\\Windows\\notepad.exe", 0, 20);
}
Para simplificar el ejemplo no realicé ningún tipo de comprobación (existencia del archivo, valor de los argumentos, etc)
Saludos