循环打开头文件

来源:互联网 发布:苏州二手房成交数据 编辑:程序博客网 时间:2024/05/20 05:27
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include<string.h>#include<windows.h>#define MAXLINE 1024void 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);    }}

0 0
原创粉丝点击