Xael Yvette Xael Yvette - 3 months ago 17
C Question

Re: Reading a line and using realloc in C

So I browsed some of the same questions and found a working answer. I was just wondering if someone can explain to me the last line of the code from this answer:

Read text from a file and realloc when needed

fgets(linebuffer + (maxlinelen/2 - 1), maxlinelen/2 + 1, fp); //this line


Thank you very much!

Answer

When he double the capacity first half of the linebuffer is fill, the other half is empty. Because of that, he is writing data starting from middle of the linebuffer. Thus he do not overwrite to first half of the linebuffer