你的位置:首页 > 信息动态 > 新闻中心
信息动态
联系我们

Linuxc动态读取多个文件并提取每个文件里的部分内容

2021/12/6 16:05:26

这个东西流程就是:先find找文件,放到一个文本里,然后从文本里一行一行读取路径,然后再打开,打开后进行取数据操作,核心函数就fgets

我这个是读取的xml,相当于匹配字符  '<'

#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <errno.h>
#define N 1024*1024

int main()
{
        FILE *fp_b;
        FILE *fp_a;
        FILE *fp_c;
        char b[N];
        char * excel=malloc(1024*1024*1024*sizeof(int));
        int l=0;
        int z=20;
        int xiaoyuhao =0;
        char c[N];
        char a[N];    
        strcat(excel,"<DEVICE_VALIDATIONS>");
        system("find . -name *.xml >2.txt");
        fp_b = fopen("2.txt","r");
        if (fp_b == NULL)
            {
                printf("open 1.txt faild");
                exit(1);
            }
        while (!feof(fp_b)) 
        {
            fgets(a, sizeof(a), fp_b);        
            char * filename = a;
            char *tmp = NULL;
        if ((tmp = strstr(filename, "\n")))
         {
        *tmp = '\0';
         }
            fp_a = fopen(a,"r");
            if (fp_a == NULL)
            {
                printf("%s %d\n",filename,errno);
                perror("perror");
                printf("open a faild\n");
                exit(1);
            }
            while (!feof(fp_a)) 
            {
                fgets(c, sizeof(c), fp_a);
                for(int i=0;i<=sizeof(c);i++)
                {
                    if (c[i]==60)
                        {
                            xiaoyuhao++;
                        }
                    if ((xiaoyuhao>1) && (xiaoyuhao<6))
                        {
                                        *(excel+z)=c[i];
                                        z++;
                        }        
                }
                    //printf("excel= %s\n",excel);
            }
                l++;
                xiaoyuhao=0;
        }
        strcat(excel,"</DEVICE_VALIDATIONS>");
        fp_c = fopen("mubiao.xml","w+");
        if (fp_c == NULL)
            {
                printf("open mubiao.xml faild");
                exit(1);
            }
            fwrite(excel,strlen(excel),1,fp_c);
        fclose(fp_a);
        fclose(fp_b);
        fclose(fp_c);
        printf("%d\n",z);
        return 0;
    }