@bogomil
That
for loop looks like it is really for removing spaces from the
beginning of the line rather than the
trailing spaces as the comment says. The problem with it is that the index variable
j is never reset for the next line. So, putting the initializer
j=0 into the for loop should fix that. Personally, I would have just used a
while loop for this:
Code:
while (*p == ' ') p++;
Also,
fgets keeps the newline character at the end of the string. Maybe that's what's causing the strange characters to appear in the contents. You should strip it off the back of the string if it is there.
Finally, it's worth checking to see if
i>0 before calling
OpenContents, otherwise it hangs the program if there are no lines to show.
Edit: I forgot to add that files with more that 512 lines are going to start corrupting your program, because you will be writing past the end of
toc. You need to
realloc more space before then.