Doubt entry operator ""

1

I have the following function:

CLliga::CarregaDades(const char* nomFitxer)
{
ifstream  fitxer(nomFitxer);

    if (fitxer.is_open())
    {
        int i = 0;
      (1)  fitxer >> m_numJornadas;

        delete[] m_pJornadas;

        m_pJornadas = new CJornada[m_numJornadas];

        for(i=0; i<m_numJornadas; i++)
        {
        (2)   fitxer >> m_pJornadas[i];
        }
        fitxer.close();
    }
}

My doubt is in both (1) and (2), I know that it calls an operator located in another class, but I do not understand what it does exactly once in the operator. It saves in a variable fitxer the value of numjornadas and then overwrite it with the values of the array m_pJornadas?

    
asked by ElPatrón 23.03.2017 в 21:18
source

1 answer

2
ifstream  fitxer(nomFitxer);

Create a file stream with the last name as an argument. Open a file, come on.

If the file has been opened correctly ...

fitxer >> m_numJornadas;

The declaration of m_numJornadas is not observed, but, from the following lines, we deduce that it is a int (or similar, a numeric variable, come on).

That line reads a file number previously opened.

delete[] m_pJornadas;
m_pJornadas = new CJornada[m_numJornadas];

m_pJornadas is a pointer , of type CJornada * ;

Previous lines release the memory block previously pointed to by m_pJornadas , and assign a new memory block, capable of containing m_numJornadas (whose value we previously obtained from the file).

fitxer >> m_pJornadas[i];

Inside the loop, filling the previously reserved memory block with the CJornada s data that we are obtaining from the file.

In summary: open the file, read the number of elements to be read, assign a memory for that number of elements, and read and place them in the above memory block.

We had a list of elements, and replaced it with a new list of elements.

EDITO

To respond to comments:

What tells you which line of the document to read?

Start at the beginning; it reads right after opening it, so it starts with the byte 1 of the file.

The first reading, unless the operator ifstream::operator>>( ) is rewritten, reads an integer; Size? Until it reaches \n .

How often does it go down to the next line?

As you are reading CJornada s, we do not know ; we do not know if it is a typedef of some basic type, or a class of its own. In the latter case, you may have defined the ofstream::operator>>( std::ofstream &, CJornada & ) , so you can read the way that class considers convenient: byte to byte , line by line (up to the \n ), 300 in 300 bytes , ...

    
answered by 23.03.2017 / 21:32
source