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

发布于 2025-01-02 19:52:13 字数 2664 浏览 3 评论 0原文

我正在尝试编辑文本文件中的一行,但在编辑文件时出现意外行为。我想要做的是调整看起来像的文本的特定行(点:100)。在函数中,我按值传递参数、要调整的新硬币以及 ftell->user_point 的文件偏移量。我得到的输出很奇怪。我尝试将文件的其余部分复制到临时文件中,并编辑行,然后将其从我复制到临时文件的点复制回原始文件。(即与ftell 的 user_point 偏移量)。 这是带有这样的条目的原始文件:

...    
_______________________________________
    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 \n ,至少我是这么认为的,这是由于 "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,'\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);

}

我已经调试过该程序和一切似乎至少在将哪些值传递给我存储行字符的数组上起作用,但我不明白为什么当我尝试复制它时它会忽略上一行的 \n回到原来的样子……好像有一个\r 字符,当我复制回原始文件时我无法删除它...... 提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2025-01-09 19:52:13

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文