首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

新手各位大神!主要是文件的读取

2013-09-28 
新手求助各位大神!主要是文件的读取每个文件的结构都是一样的,(文件中第35行,epoch1一直到epoch6下面都

新手求助各位大神!主要是文件的读取

每个文件的结构都是一样的,(文件中第35行,epoch=1一直到epoch=6下面都没有数据,但是其他文件是可能有的),我现在的任务就是要把所有的txt文件中epoch=1下面的数据放在一个自己新生成的txt文件里,同样所有txt文件epoch=2下面的数据放在一个txt文件里,其他的epoch=3,4......都类似处理(共2881个)。
希望各位大神能帮帮小弟这个忙啊!非常感谢!!
[解决办法]
菜鸟,帮顶了,不过我感觉这个好像不难,比我要做的二进制文件读取容易多了
[解决办法]
没实际编译链接调试,不一定对,仅供参考:

//假定输入文件在d:\in\*.txt,输出文件放在d:\out\1..2881.txt
#include <stdio.h>
#include <string.h>
char fn[256];
char ln[256];
char fpn[256];
char fon[256];
FILE *af;
FILE *fi;
FILE *fo;
int L,e,s;
int main() {
    system("dir /b /a-d d:\\in\\*.txt >d:\\allfn.txt");
    af=fopen("d:\\allfn.txt","r");
    if (NULL==af) {
        printf("Can not open file d:\\allfn.txt!\n");
        return 1;
    }
    while (1) {
        if (NULL==fgets(ln,256,af)) break;
        L=strlen(ln);
        if ('\n'==ln[L-1]) ln[L-1]=0;
        printf("read %s\n",ln);
        strcpy(fpn,"d:\\in\");
        strcat(fpn,ln);
        fi=fopen(fpn,"r");
        if (NULL==fi) {
            printf("Can not open file %s!\n",fpn);
            fclose(af);
            return 3;//之前的malloc在main退出后由操作系统自动free
        }
        e=0;
        s=0;
        while (1) {
            if (NULL==fgets(ln,256,fi)) break;
            if (1==sscanf(ln,"epoch=%d",&e)) {
                if (e>0) fclose(fo);
                sprintf(fon,"d:\\out\\%d.txt",e);
                fo=fopen(fon,"a");
                s=1;
            } else if (s==1) fprintf(fo,"%s",ln);
        }
        if (e>0) fclose(fo);
        fclose(fi);
    }
    fclose(af);
    return 0;
}

热点排行