读一个文件里的某个值,然后写到另外一个文件内。

来源:互联网 发布:seo主管招聘 编辑:程序博客网 时间:2024/05/21 14:40

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

int main(int argc, char **argv)

{
    FILE *fp;
    FILE *fpconf;
    FILE *fpw;
    int filenum;
    int rc;
    int size = 0;
    char buff[8192];
    char *aimbuf;
    char *bimbuf;
    char aimbuff[200];
    char *infpname;
    char *outfpname;

    printf("the argc is %d/n", argc); 
    infpname = argv[1];      //input file
    printf("infpname is %s/n", infpname);
    outfpname = argv[2];   //output file
    printf("outfpname is %s/n", outfpname);

    fpconf = fopen(outfpname, "rb");
    if (fpconf == NULL)
        printf("open write file error/n");
    while (fgets(buff, 8192, fpconf) != NULL) {
        bimbuf = strstr(buff, "ro.product.model"); // to find whether the "ro.product.mode" is exist in the file.
        if (bimbuf == NULL) {
       
       

            fclose(fpconf);

            fp = fopen(infpname, "rb");
            fpw = fopen(outfpname, "a");
            if (fp == NULL)
                printf("open readfile error/n");
            if (fpw == NULL)
                printf("open write file error/n");

            while (fgets(buff, 1024, fp) != NULL) {// to read the string which after the string <VALUE>
                aimbuf = strstr(buff, "<VALUE>");
                if (aimbuf) {
                    rc = 0;
                    while (*aimbuf != '>')
                        aimbuf++;
                    aimbuf++;
                    printf("%s/n", aimbuf);
                    while (*aimbuf != '<') {// return the value until "<" is found.



                        aimbuff[rc++] = *aimbuf;
                        aimbuf++;
                    }
                    printf("aimbuff is %s/n", aimbuff);

                    fprintf(fpw, "echo /"ro.product.model= %s/"/n", aimbuff);
                    break;
                }
                memset(aimbuff, 0, 100);
            }
            //    free(buff);
            fclose(fp);
            fclose(fpw);
        }
    }
    return 0;

}

 

 

本程序实现了,从一个input file中读出<VALUE>XXX</VALUE>的内容。写如output file 中。

在写之前,先读output file,看这个值是否存在。