I have written the following piece of code:
#include<stdio.h>
void add_linebreak()
{
FILE *fp ;
char c, NEWL=10 ;
fp=fopen("text1.txt","a+") ;
if(fp==NULL)
{
printf("\nFile Not Found") ;
}
fseek(fp,0,SEEK_CURR) ;
while(!feof(fp))
{
//if(!feof(fp))
//{
c=fgetc(fp);
if(c==NEWL)
{
fprintf(fp,"%c",NEWL) ;
}
//}
}
fclose(fp) ;
}
int main()
{
add_linebreak() ;
printf("\nEditing Complete") ;
return 0 ;
}
The program took following data as input from a file named text1.txt :
1 this 2 is 3 a 4 text 5 file 6 to 7 test a 8 program 9 written 10 in c
Actual Output :
1 this 2 is 3 a 4 text 5 file 6 to 7 test a 8 program 9 written 10 in c 11
Expected Output:
1 this 2 3 is 4 5 a 6 7 text 8 9 file 10 11 to 12 13 test a 14 15 program 16 17 written 18 19 in c 20
I scratched my head on this for hours but wasn't able to get the expected output, please help me.