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 question is both in (1) and (2), I know that it calls an operator located in another class, but I don't understand what it does exactly once in the operator. Do you save the value of numdays in a fitxer variable and then overwrite it? with the values of the array m_pJornadas?
Create a file stream with the name passed as an argument. Open a file, come on.
If said file has been opened correctly...
The declaration of , is not observed
m_numJornadas
, but, from the following lines, we deduce that it is aint
(or similar; a numerical variable, come on).That line reads a number from the previously opened file.
m_pJornadas
is a pointer , of typeCJornada *
;The above lines free the block of memory previously pointed to by
m_pJornadas
, and allocate a new block of memory, with the capacity to holdm_numJornadas
(the value of which we previously obtained from the file).Inside the loop, it fills up the previously reserved memory block with the
CJornada
s data that we are getting from the file.In short: it opens the file, reads the number of elements to read, allocates memory for said number of elements, and reads and places them in the aforementioned block of memory.
We had a list of items, and we replaced it with a new list of items.
EDIT
To respond to comments:
What tells you which line of the document to read?
Start at the beginning; it is read right after it is opened, so it starts at byte 1 of the file.
The first read, unless the operator
ifstream::operator>>( )
is rewritten, reads an integer; size ? Until I get to\n
.How often does it go down to the next line?
Since you are reading
CJornada
s, we don't know ; we don't know if it is atypedef
of some basic type, or a class of its own. In the latter case, you may have defined theofstream::operator>>( std::ofstream &, CJornada & )
, so you can read in the way that class deems convenient: byte by byte , line by line (up to\n
), 300 by 300 bytes , ...