在* .txt文件中搜索和更新1行 - C语言

时间:2016-07-03 14:44:57

标签: c file search replace updates

我正在尝试处理文件。我需要管理* .txt文件,我必须使用关键字搜索特定行,然后编辑该行的某个字段。

MyFile.txt包含:

名称积分胜亏平局

 Mark 4 1 0 1
 Kevin 6 2 1 0
 Phill 10 3 0 1
 Tony 13 4 1 1
 Dan 12 3 2 3
 -END-

其中:名称是我的关键字,我必须编辑积分,获胜,失败和平局。

void Update (int Result, char User[15])
{
    struct  Giocatore Player;
    int temp;
    FILE *fp;


    fp=fopen("MyFile.txt","r+");
    if(fp==NULL)
        {
            printf("ERROR.");
       }
    else
        {
            //reading first line of txt
            fscanf(fp,"%s %d %d %d %d",Player.Name,&Player.pts,&Player.wins,&Player.loss,&Player.tie);
            do
                {
                    if(strcmp(Player.Name, User)==0) //finding the username got from main.
                        {
                            if(Result==1) //win root
                                {
                                    Player.Wins++;
                                    Player.pts=(Player.wins*3)+Player.tie;
                                    fprintf(fp,"%s %d %d %d %d",Player.Name,Player.pts,Player.wins,Player.loss,Player.tie);
                                }
                            else if(Result==0) //Tie root
                                {
                                    Player.tie++;
                                    Player.pts=(Player.wins*3)+Player.tie;
                                    fprintf(fp,"%s %d %d %d %d",Player.Name,Player.pts,Player.wins,Player.loss,Player.tie);
                                }
                            else if(Result==2) //loss root
                                {
                                    Player.loss++;
                                    fprintf(fp,"%s %d %d %d %d",Player.Name,Player.pts,Player.wins,Player.loss,Player.tie);
                                }
                        }
                    fscanf(fp,"%s %d %d %d %d",Player.Name,&Player.pts,&Player.wins,&Player.loss,&Player.tie);

                    temp=strcmp(Player.Name,"-END-");

                    } /* end while*/
                while(temp!=0);
            fclose(fp);
        }

}

我使用此代码已经制作但它不起作用,我能够找到用户名但我无法更新它。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您可以保存行号,然后使用行号写入包含新内容的文件副本。

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

struct Giocatore {
    char Name[15];
    int pts;
    int wins;
    int loss;
    int tie;
    int Wins;
};

int replaceline(int lineNum, char *replacement) {
    FILE *fp, *fc;

    int count = 0;  //count number lines in source file.
    int ch;   //temporary place to store character of source file(one at a time).
    int edited = 0;  //0=false and 1=true
    char *t;   //temporary place to store input which you want to be replaced with error in text file.


    fp = fopen("MyFile.txt", "r");
    fc = fopen("output.txt", "w");

    if (fp == NULL || fc == NULL) {
        printf("\nError...cannot open/create files");
        return 1;
    }


    while ((ch = fgetc(fp)) != EOF) {
        if (ch == '\n')
            count++;
        if (count == lineNum - 1 && edited == 0) {


            if (count == 0)
                fprintf(fc, "%s\n", replacement);
            else
                fprintf(fc, "\n%s\n", replacement);

            edited = 1;

            while ((ch = fgetc(fp)) != EOF) {
                if (ch == '\n')
                    break;
            }
        }
        else
            fprintf(fc, "%c", ch);
    }
    fclose(fp);
    fclose(fc);

    if (edited == 1)
        printf("\nCongrates...Error Edited Successfully.");
    else
        printf("\nLine Not Found");

    return 0;
}


void Update(int Result, char User[15]) {
    struct Giocatore Player;
    int temp;
    FILE *fp;
    int line = 0;
    char ch[15];
    fp = fopen("MyFile.txt", "r+");
    if (fp == NULL) {
        printf("ERROR.");
    }
    else {
        //reading first line of txt
        fscanf(fp, "%s %d %d %d %d", Player.Name, &Player.pts, &Player.wins, &Player.loss, &Player.tie);
        do {
            line++;
            printf("Player name %s ", Player.Name);
            if (strcmp(Player.Name, User) == 0) {
                if (Result == 1) {
                    Player.wins++;
                    Player.pts = (Player.wins * 3) + Player.tie;

                    sprintf(ch, "%s %d %d %d %d", Player.Name, Player.pts, Player.wins, Player.loss, Player.tie);
                    printf("%s\n", ch);
                    break;


                }
                else if (Result == 0) //Tie root
                {
                    Player.tie++;
                    Player.pts = (Player.wins * 3) + Player.tie;

                }
                else if (Result == 2) //loss root
                {
                    Player.loss++;

                }
            }
            fscanf(fp, "%s %d %d %d %d", Player.Name, &Player.pts, &Player.wins, &Player.loss, &Player.tie);

            temp = strcmp(Player.Name, "-END-");

        } /* end while*/
        while (temp != 0);
        fclose(fp);

        replaceline(line, ch);
    }

}

int main(void) {
    Update(1, "Kevin");
    return 0;
}

档案MyFile.txt

Mark 4 1 0 1
Kevin 6 2 1 0
Phill 10 3 0 1
Tony 13 4 1 1
Dan 12 3 2 3
-END-

程序运行后的文件output.txt(Kevin的分数已更新)

Mark 4 1 0 1
Kevin 9 3 1 0
Phill 10 3 0 1
Tony 13 4 1 1
Dan 12 3 2 3
-END-