编辑特定行中的一个文件 [英] Edit a specific row in a file

查看:113
本文介绍了编辑特定行中的一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个TXT文件:

 鞋:12
裤:33
外套:26
眼镜:16
T恤:182

我需要编辑夹克数(26日至42为例)。
所以,我写了这个code,但我不知道怎么哪里有字编辑特定行外套:

 的#include<&stdio.h中GT;诠释主(){
    炭行[256];
    FILE * FP;    如果(!(FP =的fopen(myfile.txt的,RW))){
        的printf(错误);
        返回1;
    }    而(!的feof(FP)){
        与fgets(行,256,FP);
        //如果有外套在此行中,然后编辑该行
    }    FCLOSE(FP);
    返回0;
}


解决方案

不幸的是没有简单的解决办法这一点。

一个常用的方法是写出所有行(改性或没有)​​到一个临时文件,然后将临时文件在现有的文件

I have a txt file like this:

"shoes":12
"pants":33
"jacket":26
"glasses":16
"t-shirt":182

I need to edit the number of jacket ( from 26 to 42 for example ). So, I have wrote this code, but I don't know how to edit a specific row where there is the word "jacket":

#include <stdio.h>

int main() {
    char row[256];
    FILE *fp;

    if (!(fp=fopen("myfile.txt","rw"))) {
        printf("Error");
        return 1;
    }

    while (!feof(fp)){
        fgets(row, 256, fp);
        // if there is the "jacket" in this row, then edit the row
    }

    fclose (fp);
    return 0;
}

解决方案

Unfortunately there is no easy solution to this.

A common method is to write all lines (modified or not) to a temporary file, and then move the temporary file over the existing file.

这篇关于编辑特定行中的一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆