如何在文件中编辑某些值?

时间:2016-12-10 16:55:42

标签: c file int edit

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

#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

1 个答案:

答案 0 :(得分:1)

没有预定的方法可以做到这一点。由于您暗示您的文件是普通的无格式文本,因此您需要处理短语&#34; 12月交易和#34;和&#34; 11月的交易&#34;好像它们是分隔符或标签。

解决方案的一个示例是在代码中添加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");
}