使用临时文件C编辑文本文件中的一行



我试图编辑文本文件中的一行,但在编辑文件时出现了意外行为。我想做的是调整文本中看起来像的特定行(点:100)。在函数中,我通过ftell->user_point按值传递要调整的新硬币和文件的偏移量。我得到的输出很奇怪。我试着用编辑过的行将文件的其余部分复制到一个临时文件中,然后从复制到临时文件的点将其复制回原始文件。(这是ftell的用户点偏移量)。这是原始的封地,条目如下:

...    
_______________________________________
    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
...

第二次编辑后我得到的是:

...
    _______________________________________
    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 :/

所以进一步的编辑会破坏文本。。。我在行的末尾也得到了一个EXTRA,至少我认为是由于"r+"模式,这也是我不想要的。。。

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,'',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,'',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,'',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);
}

我已经调试了这个程序,所有的东西似乎都能工作,至少在我存储行字符的数组中传递了什么值,但我不明白为什么当我试图将前一行的n复制回原始行时,它会忽略它。。。当我复制回原始文件时,似乎有一个r字符无法删除。。。提前谢谢。

我更多的是考虑这样的事情:

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: %dn", "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);
}

它更短、更简单,也许更有效(逐行循环,而不是逐个字符循环),并且您要查找的行可以在文件中的任何位置,而不知道它的确切位置。

最新更新