【问题标题】:How to edit certain value in file?如何编辑文件中的某些值?
【发布时间】:2017-04-25 23:13:24
【问题描述】:

我只想编辑文件中的某些值,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    int month[3]={0,0,0};
    int month1;
    printf("\n1.December");
    printf("\n2.November");
    printf("\nEnter month:");
    scanf("%d",&month1);

    if(month1 == 2)
    {
        printf("\nthis is November");
        month[1]=3*5;
        printf("\ntotal for this month is :%d",month[1]);
    }

        else if (month1 == 1)
    {
        printf("\nthis is December");
        month[2]=2*5;
        printf("\ntotal for this month is :%d\n\n\n",month[2]);

    }

    FILE *mo;
    if( (mo = fopen ("month.txt", "w" ) ) == NULL)
        {
        printf ("File unable to open");
        } // end if
    else 
    {
        fprintf(mo,"Transaction for December is %d ",month[2]);
        fprintf(mo,"\nTransaction for November is %d ",month[1]);

    }


return 0;
system("pause");
}

假设我运行这个程序一次,然后程序将把这个值存储在文件中。 以及下面文件month.txt的内容

12 月的交易是 10 日 11 月的交易为 0

如何使,在我再次运行该程序后,10 的值不会改变与 0 的关联,它会自动更新该值。 并使其在文件month.txt中看起来像这样 (在我运行了三遍这个程序之后) 首先我选择 1 第二和第三我选择 2 ,

12 月的交易是 10 日 11 月的交易是 30

【问题讨论】:

    标签: c file int edit


    【解决方案1】:

    没有预先确定的方法来做到这一点。由于您暗示您的文件是纯无格式文本,因此您需要将短语“Transaction for December”和“Transaction for November”视为分隔符或标签。

    解决方案的一个示例是将while 循环添加到扫描整个文档的代码中,然后替换必要的字符串。我建议的实现将使用fgets 拉出一条线,然后由strstr 分析。当然,您也可以使用各种其他功能,例如strtok

    但是,应该注意的是,这不一定是安全的操作。最好让您的程序从源文件中复制每一行并将它们打印在完全不同的文件中。然而,行为将大致相同。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
        int month[3] = { 0,0,0 };
        int month1;
        printf("\n1.December");
        printf("\n2.November");
        printf("\nEnter month:");
        scanf("%d", &month1);
    
        if (month1 == 2)
        {
            printf("\nthis is November");
            month[1] = 3 * 5;
            printf("\ntotal for this month is :%d", month[1]);
        }
    
        else if (month1 == 1)
        {
            printf("\nthis is December");
            month[2] = 2 * 5;
            printf("\ntotal for this month is :%d\n\n\n", month[2]);
    
        }
    
        FILE *mo = fopen("month.txt", "w");
        if (mo == NULL)
        {
            printf("File unable to open");
        } // end if
        else
        {
            // Value to edit
            char phraseToUpdate[] = "Transaction for December is ";
            char currentLine[50];
    
            // Scan an initial line
            fgets(currentLine, 49, mo);
    
            // Look at every line of the file
            while (strchr(currentLine, EOF) == NULL)
            {
                // Find your phrase
                if (strstr(currentLine, phraseToUpdate) != NULL)
                {
                    // Write to your file
                    // You WILL have to modify these to update on the current line
                    // fprintf(mo, "Transaction for December is %d ", month[2]);
                    // fprintf(mo, "\nTransaction for November is %d ", month[1]);
                }
                else
                {
                    // Put that thing back where it came from (or so help me)
                    fprintf(mo, currentLine);
                }
    
                // Scan next line
                fgets(currentLine, 49, mo);
            }
    
        }
    
    
        return 0;
        system("pause");
    }
    

    【讨论】:

      猜你喜欢
      • 2012-01-10
      • 2012-10-18
      • 2021-12-25
      • 2021-08-11
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 2015-09-16
      相关资源
      最近更新 更多