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

Posted

技术标签:

【中文标题】在 *.txt 文件中搜索和更新 1 行 - C 语言【英文标题】:Search and update 1 line in a *.txt file - C Language 【发布时间】:2016-11-05 09:06:39 【问题描述】:

我正在尝试处理文件。我需要管理一个 *.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】:

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

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

【讨论】:

以上是关于在 *.txt 文件中搜索和更新 1 行 - C 语言的主要内容,如果未能解决你的问题,请参考以下文章

如何告诉搜索引擎使用我更新的robots.txt文件?

在 C 中读取和打印 .txt 文件行的最清晰方法

Linux:从文件中搜索关键字并显示行数(cat,grep函数)

c ++ txt文件逐行字符串和int用于纸牌游戏

python 逐行读取txt文件里的词, 并反复搜索 如何实现?

Findstr:在txt文件文件夹中搜索字符串列表