this is the code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fpin, *fpout;
int ch,position;
unsigned line=1;
char filename[100];
printf("Please enter the file name\n");
scanf("%s",&filename);
if((fpin=fopen(filename,"r")) == NULL)
{
printf("Couldnt open file %s for input\n",filename);
exit(1);
}
if((fpout=fopen("res.txt","w")) == NULL)
{
printf("Couldnt open \"res.txt\" for output\n");
exit(1);
}
fprintf(fpout,"%d ",line);
while ((ch=getc(fpin)) != EOF)
if( ch == '\n')
{
putc(ch,fpout);
fprintf(fpout,"%d ",++line);
}
else
putc(ch,fpout);
fclose(fpin);
fclose(fpout);
return 0;
}
$ more input.txt
this is the first line
this is the second line
this is the third line
$ more res.txt
1 this is the first line
2 this is the second line
3 this is the third line
4
It seems that something happens and it cant read the EOF char.
OR even it reads it does something wrong.
I cant understand why.
I ve double-checked the input file if it has another new line char but it doesnt have one. I ve checked it with vi as well and there is no other char after the last char.
Could you please help me on this strange issue ?
|