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

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

}

我正在使用我制作的这段代码,但它不起作用,我能够找到用户名,但我无法更新它。

感谢帮助

最佳答案

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

#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-

关于c - 在 *.txt 文件中搜索并更新 1 行 - C 语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171057/

相关文章:

c - C 中的二进制文件

c - C结构的对齐

c++ - 如何在 Linux 上使用 C++ 中的 read() 和 O_DIRECT 读取文件?

performance - 计算数组中“小于x”的元素

typescript - 有没有办法使用 typescript 和 Sequelize 向查询添加选项?

c - 如何使用 fwrite 将结构写入文件?

c - 动态数组 : Separating Numbers into Even and Odd Using C

java - 使用 Java 将文本文件读入数组会生成异常

php - 读取没有最后 x 字节的文件

javascript - 如何使 Apostrophe/Node.js 内容页面可抓取?