循环打开头文件

#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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值