Add feof(fold) == 0 checks in main loop for preventing segfaults.