Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7414997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:12:43+00:00 2026-05-29T07:12:43+00:00

I am trying to edit a line in a textfile but i have an

  • 0

I am trying to edit a line in a textfile but i have an unexpected behavior while i am editing the file. What i want to do is adjust a specific line (points : 100) of a text that looks like. In the function i pass arguments by value the new coins to be adjusted and the offset of the file with ftell->user_point. What i get as an output is weird. I try to copy the rest of the file to a temp,with an edited line, and then copy it back to the original file from the point that i copied to temp.(thats the user_point offset with ftell).
Here is the original fie with entries like that:

...    
_______________________________________
    nickname     : geo
    password     : cuvctq
    Name         : george
    Surname      : papas
    points       : 100
    participated : 
    past draws   : 0
    Chosen No.   : 
    future draws : 0
    Registered   : Sun Feb 05 19:23:50 2012
...

What i get after 2nd edit run is:

...
    _______________________________________
    nickname     : geo
    password     : cuvctq
    Name         : george
    Surname      : papaspoints       : 98
    participated : 
    past draws   : 0
    Chosen No.   : 
    future draws : 0
    Registered   : Sun Feb 05 19:23:50 2012
...
At the end of the text i get one extra \n after i edit the 
file whch is something i dont want :/

and so further edit will spoil the text…
I also get an EXTRA \n at the end of the line which, at least what i think so, is due to "r+" mode which is something that i also dont want…

void coins_adjust(int coins_new,int user_point)
{
    int lines,i,ln_point_copy;
    char buffer[50],buff_copied[50];
    FILE *lottary,*temp;

    memset(buff_copied,'\0',sizeof(char)*50);
    lottary=fopen("customers.txt","r");
    temp=fopen("temp.txt","w");
    fseek(lottary,user_point,SEEK_SET);
    for (lines=0;lines<5;lines++)
    {
        memset(buffer,'\0',sizeof(char)*50);
        if (lines==5)
            ln_point_copy=ftell(lottary);       //from TEMP to CUSTOMERS
        fgets (buffer ,50 , lottary);
    }
    coins_new+=atoi(buffer+15);

    strncpy(buff_copied,buffer,15);     //copy 15 chars and fill with null
    memset(buffer,'\0',sizeof(char)*50);
    itoa (coins_new,buffer,10);          //fix the new line to be entered
    strcat(buff_copied,buffer);          //the edited line is as it is supposed
    strcat(buff_copied,"\n");            //to be with \n at the end.
    puts(buff_copied);

    printf("%s",buff_copied);fflush(stdout);
    fprintf(temp,"%s",buff_copied);
    for(i=getc(lottary); i!=EOF; i=getc(lottary))  //copy to temp
    {
        putc(i, temp);
    }
    fclose(lottary);
    fclose(temp);

    temp=fopen("temp.txt","r");
    lottary=fopen("customers.txt","r+");
    fseek(lottary,ln_point_copy,SEEK_SET);
    for(i=getc(temp); i!=EOF; i=getc(temp))     //copy until eof
    {
        putc(i, lottary);
    }
    fclose(lottary);fclose(temp);

}

I have debugged the program and everything seems to work at least on what values are passed to the arrays where i store the line chars but i cant see why it ignores the \n of the previous line when i try to copy it back to the original… There seems to be a \r char that i cant get rid of while i copy back to the original…
Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:12:44+00:00Added an answer on May 29, 2026 at 7:12 am

    I was more thinking about something like this:

    void change_points(int new_points)
    {
        FILE *input  = fopen("customers.txt", "r");
        FILE *output = fopen("temp.txt", "w");
    
        char buffer[256];
    
        while (fgets(buffer, sizeof(buffer), input))
        {
            /* Look for the correct line */
            /* Can also use e.g. "if (strncmp(buffer, "points", 6) == 0)"
             * if it's at the start of the line
             */
            if (strstr(buffer, "points") != NULL)
            {
                int old_points;
    
                sscanf(buffer, "%*s : %d ", &old_points);
    
                /* Format how you like it */
                fprintf(output, "%-13s: %d\n", "points", new_points + old_points);
            }
            else
                fputs(buffer, output);
        }
    
        fclose(output);
        fclose(input);
    
        /* The file "temp.txt" now contains the modifeed text */
        /* Copy either using "fgets"/"fputs", or using "fread"/"fwrite" */
    
        input  = fopen("temp.txt", "r");
        output = fopen("customers.txt", "w");
    
        while (fgets(buffer, sizeof(buffer), input))
            fputs(buffer, output);
    
        fclose(output);
        fclose(input);
    }
    

    It’s shorter, simpler, maybe more effective (looping over line-by-line instead of char-by-char), and the line you are looking for can be anywhere in the file without you knowing its exact position.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form which sends data to a text file, but I'm trying
I am trying to edit a line of a text file (.Hex file) containing
I'm trying to edit the httpd.conf file located in /private/etc/apache2, and I can't figure
I'm trying to edit the navigation bar but the last li is a little
I am trying to create a simple batch file that will replace a line
I'm trying to use PHP to create a file, but it isn't working. I
I'm trying to edit my sidebar.php file in my current them WP is using
I am trying to put on the same line $('.prize-item input') and $('.prize-edit') Here
I'm trying to edit a record in the database, but the script gives me
I have a bunch of .txt files I am trying to read but for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.