Read a memory address stored in a text file


I need to read the memory address of a pointer from a file, since I implemented the function, an integer function that when reading the file it returns the stored address:

int Cargar(std::string nombre)
std::ifstream archivo;,std::ios::in);

    std::cout<<"No se pudo cargar el archivo."<<std::endl;
    return 0;

int direccion;


return direccion;


int main()
  int direccion = Cargar("Direcciones.txt");

  return 0;

When I print on the screen the result is always 0.

Note: in the file "Directions.txt", only the following line is found:

asked by M1L0 13.08.2018 в 00:58

3 answers


Update: in the end I only needed to use "std::hex" to read the file, although optionally if for some reason they need to convert a string that stores a hexadecimal value to an integer variable, they can use "std::stoi" in the following way:

int entero = std::stoi(texto.c_str(),nullptr,16);

With the third parameter we are indicating that we convert the integer to base 16 (hexadecimal).

answered by 19.08.2018 / 00:32

If your intention is to read a number ... Why do you read first in a char[] ? You can save this step and read directly in an integer:

int direccion;
archivo >> direccion;

std::cout << direccion << '\n';

return direccion;

Among other things, stoi does not convert from hexadecimal to base 10, then if the values are in this format, the number read will be always 0 ( the first digit of the hexadecimal number):

 ^ la x ya no es un digito, deja de leer
^ Primer digito numerico
answered by 13.08.2018 в 07:59

I think, the problem is in the conversion of char * to int. In C I solved it in the following way:

#include <stdio.h>
#include <stdlib.h>

int cargar(FILE* arch)
    int retorno;
    char buffer[500];
    fgets(buffer, 500, arch);
    sscanf(buffer, "%x", &retorno);
    return retorno;

int main()
    FILE* arch = fopen("memory.txt", "rt");
    if(!arch) return 0;
    int retorno = cargar(arch);
    printf("El retorno es: %x", retorno);
    return 0;

The output on the screen was as follows:

You can also use this information SOURCE .

answered by 13.08.2018 в 03:30