0

So i have some code that will allow the user to write anywhere they want inside a text file, thanks to the answers from How do I write to a specific line of file in c? , However i have hit a new obstacle, whenever i write back to the file an annoying random character will always appear at the end of the last word, and if it's on the first line a new line is created before it. I know this has something to do with the file copy but i don't know where, can someone please help?

int main()
{
FILE *fp,*fc;
int lineNum;  
int count=0;  
int ch=0;   
int edited=0; 
char t[16];  


fp=fopen("start.txt","r");
fc=fopen("end.txt","w");

if(fp==NULL||fc==NULL)
{
    printf("\nError...cannot open/create files");
    return 1;
}

printf("\nEnter Line Number Which You Want 2 edit: ");
scanf("%d",&lineNum);

while((ch=fgetc(fp))!=EOF)
{
    if(ch=='\n')  
        count++;
    if(count==lineNum-1 && edited==0)   
    {
        printf("\nEnter input to store at line %d:",lineNum);

        scanf(" %s[^\n]",t); 

        fprintf(fc,"\n%s\n",t); /

        edited=1;  

        while( (ch=fgetc(fp))!=EOF )  
        {                           
            if(ch=='\n')
                break;
        }
   }
   else
      fprintf(fc,"%c",ch);
}
fclose(fp);
fclose(fc);

if(edited==1)
{
    printf("\nCongrates...Error Edited Successfully.");

    FILE *fp1,*fp2;
    char a;
    system("cls");

    fp1=fopen("end.txt","r");
    if(fp1==NULL)
    {
    puts("This computer is terrible and won't open end");
    exit(1);
    }

    fp2=fopen("start.txt","w");
    if(fp2==NULL)
    {
    puts("Can't open start for some reason...");
    fclose(fp1);
    exit(1);
    }

    do
    {
    a=fgetc(fp1);
    fputc(a,fp2);
    }
    while(a!=EOF);

    fclose(fp1);
    fclose(fp2);
    getch();
    }


    else
    printf("\nLine Not Found");

  return 0;
  }

(Sorry about ident, i'm in a rush)

Community
  • 1
  • 1

3 Answers3

0
do
{
    a=fgetc(fp1);
    fputc(a,fp2);
}
while(a!=EOF);

A do-while loop evaluates its condition after performing the body of the loop. In other words, this loop is writing EOF to the file, which you shouldn't be doing. EOF isn't actually a character, it's just something that gets returned by the OS when it's finished reading a file. I'm not sure what would be the end result of actually writing EOF to a file, but I'd hazard a guess this is what's causing the "annoying random character" you're talking about.

Invert the loop into a normal while loop, as follows, so that you're checking for EOF before writing anything:

while ((a=fgetc(fp1))!=EOF)
{
    fputc(a,fp2);
}
Chris
  • 4,661
  • 1
  • 23
  • 25
0

Try changing your do-while loop like,

while((a=fgetc(fp1))!=EOF)
    fputc(a,fp2);

I guess it will solve your problem.

deb_rider
  • 570
  • 2
  • 12
0

1) As you said "and if it's on the first line a new line is created before it"

To solve this problem you have to optimize the use of fprintf(fc,"\n%s\n",t); statement.

Replace this fprintf(fc,"\n%s\n",t); with below code.

if(count==0)  //if its the first line to edit..
    fprintf(fc,"%s\n",t)   //watch closely,no '\n' before %s,This will copy wihtout creating new line at beginning of line or file.
else 
    fprintf(fc,"\n%s\n",t);

2) And Your statement scanf(" %s[^\n]",t); will not work properly if you give multiple-words input.You have tried to use both ScanSet and %s fromat specifer. You should only use any one of them.

Lets understand with a code snippet:-

char t[16]="\0"; scanf(" %s[^\n]",t); //Assume that you gave input "abc efg" from keyboard printf("%s",t); // This will output only "abc" on moniter.

You should change it to some thing like this:- char t[16]="\0"; scanf(" %15[^\n]",t);//here 15 is sizeof(array)-1. one left to accomadate '\0'. printf("%s",t); //this will successfully output "abc efg" (if given)

Mysterious Jack
  • 621
  • 6
  • 18