循环打开头文件

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

#define MAXLINE 1024

void process_line(const char *filename);

int main(int argc,char **argv)
{
    process_line("string.h");
}

void process_line(const char *filename)
{
    char *buf,*fName, *match, *subfile;
    fName = (char *)malloc(sizeof(char)*MAXLINE);
    buf = (char *)malloc(sizeof(char)*MAXLINE);
    match = (char *)malloc(sizeof(char)*MAXLINE);
    subfile = (char *)malloc(sizeof(char)*MAXLINE);

    memset(fName,'\0',MAXLINE);
    memset(buf,'\0',MAXLINE);
    memset(match,'\0',MAXLINE);
    memset(subfile,'\0',MAXLINE);

    strcpy(match, "#include");
    strcpy(fName, "D:/softwarefile/devcpp/Dev-Cpp/MinGW32/include/");
    strcat(fName, filename);

    FILE *fp;
    FILE *file=fopen("\\Users\\jia\\Desktop\\算法案例\\chap1\\test.txt","a+");
    while((fp = fopen(fName, "r+"))==NULL)
    {
        printf("open %s error:%s\n",fName, strerror(errno));
        memset(fName,'\0',MAXLINE);
        strcpy(fName, "D:\\softwarefile\\devcpp\\Dev-Cpp\\MinGW32\\lib\\gcc\\mingw32\\4.7.2\\include\\");
        strcat(fName, filename);
        Sleep(2);
    }

    while(fgets(buf, MAXLINE, fp) != NULL)
    {
       if(strstr(buf,match) !=NULL)
       {
           strncpy(subfile, strstr(buf,"<")+1, strstr(buf,">")-strstr(buf,"<")-1);
           printf("subfile = %s\n", subfile);
           fprintf(file,"%s\n",subfile);
           Sleep(3);
           process_line(subfile);
           memset(subfile,'\0',MAXLINE);
       }
       printf("%s\n", buf);
       fprintf(file,"%s\n",buf);
       memset(buf, '\0', MAXLINE);
    }
}

点赞