How to read a line in a specific file in C ++?

1

I made an agenda where I saved data in a text file.

When you find the name of a contact, I want you to read all of your information. But I have no idea how to read just one line. I appreciate any help, thanks. (Code in codeblocks)

#include <iostream>
#include<fstream>
#include<string>

using namespace std;

struct Persona
{
    char nombre[40];
    char direccion[40];
    double telefono;
    char email[40];
    int dia;
    int mes;
    int anio;
};
typedef struct Persona contacto;

int main()
{
    contacto numero[100];
    int contador;
    char usuario[40];
    string buscador;
    cout<<"Cuantos contactos desea ingresar?";
    cin>>contador;
    cin.ignore();
    for(int i=0;i<contador;i++)
    {
        cout<<"Ingrese el nombre del usuario: ";
        cin.getline(numero[i].nombre,40);
        cout<<"Ingrese la direccion: ";
        cin.getline(numero[i].direccion,40);
        cout<<"Ingrese telefono: ";
        cin>>numero[i].telefono;
        cin.ignore();
        cout<<"Ingrese el correo electronico: ";
        cin.getline(numero[i].email,40);
        cout<<"Ingrese dia, mes y anio: ";
        cin>>numero[i].dia;
        cin>>numero[i].mes;
        cin>>numero[i].anio;
        cin.ignore();
        ofstream agendita;
        agendita.open("agenda.txt",ios::binary|ios::app);
        agendita<<numero[i].nombre<<" "<<numero[i].direccion<<" "<<numero[i].telefono<<" "<<numero[i].email<<" "
        <<numero[i].dia<<"/"<<numero[i].mes<<"/"<<numero[i].anio<<"\r\n";
        agendita.close();
    }
    cout<<"A que persona desea buscar? ";
    cin>>buscador;

    ifstream buscar;
    buscar.open("agenda.txt");
    while(!buscar.eof())
    {
     buscar>>usuario;
    if (usuario==buscador)
    {
     cout<<usuario;
    }
    }
    buscar.close();


    return 0;
}
    
asked by José T. 07.11.2016 в 15:00
source

2 answers

2

Question.

  

How to read a line in a specific file in C ++?

Can not. The lines in a text file are, in broad strokes, what is between a line break ( \r\n ) and the next ... so if you need to jump to a line you will have to read line by line and count how many you have read. That is not practical.

Alternative.

However, you are in luck because your structure Persona is of homogeneous size. So each record occupies the same in the file.

From what I see, you also open the file in binary mode ( agendita.open("agenda.txt",ios::binary|ios::app) ) so if you save the data binarily you can save them and read them en bloc:

// Guardo un registro
agendita.open("agenda.txt",ios::binary|ios::app);
agendita.write(reinterpret_cast<const char *>(&numero[i]), sizeof(Persona));
agendita.close();

So, if you need to read the contact number 42 you would do what next:

// Leo el registro 42 (creo que es Arthur Dent).
ifstream agendita("agenda.txt", ios::binary);
agendita.seekg(42 * sizeof(Persona));
Persona p;
agendita.read(reinterpret_cast<char *>(&p), sizeof(Persona));

Search?

If you do not want to write the file in binary format and you prefer to keep the text format you must first remove the ios::binary of the parameters of the stream; then you should stop worrying about reading a specific line since with your search algorithm you have it " already solved ":

while(!buscar.eof())
{
    buscar>>usuario;
    if (usuario==buscador)
    {
        cout<<usuario;
    }
}

Once you enter the if (usuario==buscador) you are already at the beginning of the registration so you can read " the specific line " of the registration data. But beware, keep in mind that your search algorithm fails if the address or email of one user matches the name of another (you will know if this is possible or not).

    
answered by 07.11.2016 / 15:45
source
1

Good morning, there is fseek , which allows you to go to a specific line of the file. here and here I leave you information about this function. also I leave you a tutorial of youtube how to use it.

This is the declaration of that function.

int fseek( std::FILE* stream, long offset, int origin );

The first parameter is the file (the stream that you have opened), the second is the position (the line you want to go to) and the third is the origin of where you are going to start counting (pricipio del archivo, final of the file)

    
answered by 07.11.2016 в 18:28