How to read .dat file and save it in an array c ++

0

I have to read a file that has this format:

Id: 23
Nombre: Roberto
Apellido: MiApellido
Id: 24
Nombre: Roberto2
Apellido: MiApellido2

And I have to read that file to be able to work with that data but I do not know how to read.

I have a struct created in the following way:

struct Datos {
    int Id;
    string Nombre;
    string Apellido;
};

How do I get that data in an array?

    
asked by Chiiviito 05.07.2018 в 18:36
source

2 answers

3

Basically you have a text file ... and reading a file of that type is one of the easiest operations to do in C ++.

With these simple instructions you read a line from a text file:

if (std::ifstream datos{"tu_archivo.dat"})
{
    std::string linea;
    std::getline(datos, linea);
}

As your data goes from 3 to 3 lines and each line follows a closed format, we can read a complete data in your structure Datos in the following way:

if (std::ifstream datos{"tu_archivo.dat"})
{
    std::string linea;
    Datos d;

    std::getline(datos, linea);
    d.Id = std::stoi(linea.substr(4));

    std::getline(datos, linea);
    d.Nombre = linea.substr(8);

    std::getline(datos, linea);
    d.Apellido = linea.substr(10);
}

If you want to store it in a data collection, just need to create it and read the file sequentially, I advise you to separate the reading process in a function:

Datos leer_un_dato_de_archivo(std::ifstream &archivo)
{
    std::string linea;
    Datos d;

    std::getline(archivo, linea);
    d.Id = std::stoi(linea.substr(4));

    std::getline(archivo, linea);
    d.Nombre = linea.substr(8);

    std::getline(archivo, linea);
    d.Apellido = linea.substr(10);

    return d;
}

This way you can make the call to the function in a loop and fill in:

int main()
{    
    std::vector<Datos> d;

    if (std::ifstream datos{"tu_archivo.dat"})
    {
        while (datos)
        {
            d.push_back(leer_un_dato_de_archivo(datos));
        }
     }

    return 0;
}

Think that this example code does not have in the gutter that the input data does not follow the format or that there are incomplete entries.

    
answered by 06.07.2018 / 11:05
source
2

Most of the data in a file can be read as normal data. In your case the only difference will be that you will have to skip the term labels that you are going to read. To be able to jump effectively it is useful to realize that all the tags (Id, Name, Surname) end with a space just before the data you want to read. We can skip these tags using the ignore function with a limit number as the first argument and a space character as a second.

Another problem to face is whether the number of elements to be read is fixed from the beginning or can change in each execution. Since you do not specify what your case is in the statement, I will assume for the moment that you already know the number in advance. If we assume that the number of elements is, for example, two, the code to read the two data to an array would be the following:

Datos arreglo[2];
for (int i = 0; i < 2; ++i) {
    archivo.ignore(100, ' '); // Salta "Id: "
    archivo >> arreglo[i].Id; // Lee el Id del elemento i

    archivo.ignore(100, ' '); // Salta "Nombre: "
    getline(archivo, arreglo[i].Nombre); // Lee el nombre del elemento i

    archivo.ignore(100, ' '); // Salta "Apellido: "
    getline(archivo, arreglo[i].Apellido); // Lee el apellido del elemento i
}
    
answered by 06.07.2018 в 10:38