C

while (!feof(f)) pitfall

Në shqip: Kurthi i feof()

ShpjegimiSQ

Shumë fillestarë shkruajnë while (!feof(f)) — por feof() bëhet true vetëm pasi leximi ka dështuar, prandaj rreshti i fundit përpunohet dy herë. Kontrollo vetë leximin: while (fgets(...) != NULL).

EnglishEN

Many beginners write while (!feof(f)) — but feof() only becomes true after a read has failed, so the last line gets processed twice. Check the read itself: while (fgets(...) != NULL).

Si ta mendosh

Si të pyesësh „a ka mbaruar libri?“ para se ta kthesh faqen, në vend që ta kthesh dhe të shohësh.

Lexoje në anglisht

Like asking “is the book over?” before turning the page, instead of turning it and seeing.

Shembull kodic

char rresht[256];
while (fgets(rresht, sizeof rresht, f) != NULL) {
    printf("%s", rresht);
}

Terma të lidhur