Extract the line from the file, concatenate it with a certain string problem

26 views Asked by At

I have a problem, in the text file I have the form "User:Username Password,Real name Real Surname Email" and so on several lines, how can I add the word "(suspended)" to the beginning of a specific line?

My code does this:

"U(suspended)ser:Username Password,Real name Real Surname Email"

How to fix it,to get?

"(suspended)User:Username Password,Real name Real Surname Email"

Here's code:

    char username_to_suspend[50];
    scanf("%s",username_to_suspend);
    char line[100];
    char new_line[sizeof(line)]="(suspended) ";
    FILE *fp = fopen("users.txt", "r+");
    while(fgets(line,sizeof(line),fp)!=NULL)
    {
        if(strstr(line,username_to_suspend)!=NULL)
        {
            strcat(new_line,&line[1]);
            fseek(fp, -strlen(line), SEEK_CUR);
            fputs(new_line, fp);
        }
        else
        {
            continue;
        }
       break;

    }

    fclose(fp);

Thanks in advance!

Best regards!

0

There are 0 answers