Fixing printtofile segmentation fault