简体   繁体   English

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

[英]Search and update 1 line in a *.txt file - C Language

I'm trying to work on files. 我正在尝试处理文件。 I need to manage a *.txt file, i have to search a specific line using a keyword, and then edit some field of this line. 我需要管理* .txt文件,我必须使用关键字搜索特定的行,然后编辑该行的某些字段。

MyFile.txt contains: MyFile.txt包含:

Names points wins loss tie 姓名点数胜负平局

 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-

Where: Names are my keyword and i have to edit points, wins, loss and tie. 其中:名字是我的关键词,我必须编辑得分,获胜,失败和平局。

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

}

Im using this code i've made but it doesn't work, I'm able to find the username but i'm not able to update it. 我正在使用我制作的这段代码,但无法正常工作,我能够找到用户名,但无法更新它。

Thanks for helping 感谢您的帮助

You can save the line number and then use the line number to write a copy of the file with the new contents. 您可以保存行号,然后使用该行号写入具有新内容的文件副本。

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

File MyFile.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-

File output.txt after program runs (Kevin's score was updated) 程序运行后文件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-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM