This program fails for the case N = 1 since the only line in the output file should be terminated with a new-line character.