Well after fixing your syntax errors (you did not compile the above) the most glaring error was that you made a string buffer and never initialized it to zero. I did that before the loop and after each use:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void){
FILE *inputFile;
FILE *outputFile;
char string[81];
memset(string, 0, 81);
int rowNr = 0;
inputFile = fopen("test.txt", "r");
outputFile = fopen ("nummering.txt", "w");
while (fgets(string, 80, inputFile) != NULL){
rowNr++;
fprintf(outputFile, "%i %s\n", rowNr, string);
memset(string, 0, 81);
}
fclose (outputFile);
fclose (inputFile);
printf("\n");
return 0;
}
Now your output file looks like:
Code:
jeff@jeff-gate:~/dev/crap$ gcc ./io.c -o iotest
jeff@jeff-gate:~/dev/crap$ ./iotest
jeff@jeff-gate:~/dev/crap$ ls
nummering.txt
io.c iotest test.txt
jeff@jeff-gate:~/dev/crap$ cat ./nummering.txt
1 Hello World.
2 How are you doing?
3 I am fine, thank you!
jeff@jeff-gate:~/dev/crap$
HTH...
Jeff