Title: C Provides a statement, such as #include filename , that reads into the file filename and inserts it into the include statement. The include statement can be nested, in other words, the file filename itself can also contain an include statement, but it is clear that a file cannot contain itself in any link. Write a program that reads into a file that is decorated by the include statement and outputs the file.
Ideas:
1. Function Printheadfile () accepts a file path and opens the path file.
2. After successful opening, continuously read into the file a row of data buf. If the row is a "#include" command line, the current data row buf (#include command line) is output when the content of the sub-file is opened and output by Printheadfile ().
3. Read in the next line of data. Closes the file until it is not read. End.
Realize:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 #defineSIZE 10246 7 voidPrintheadfile (Char*FilePath);8 9 intMainvoid)Ten { OnePrintheadfile ("D:\\Program Files (x86) \\Microsoft Visual Studio 12.0\\vc\\include\\stdio.h"); ASystem"Pause"); - return 0; - } the - /*Recursive print header file*/ - voidPrintheadfile (Char*FilePath) - { +FILE *FP; - CharSubname[size], buf[size], subpath[size] ="D:\\Program Files (x86) \\Microsoft Visual Studio 12.0\\vc\\include\\"; + Char*Pos; A intsize; at - if(Fopen_s (&FP, FilePath,"R")) - { -fprintf (stderr,"Open file failed:%s", FilePath); - return; - } in - while(Fgets (buf, SIZE, fp)! =NULL) to { + if(Strstr (BUF,"#include")) - { the if(STRCHR (BUF,'<')) * { $pos = STRCHR (buf,'<') +1;Panax NotoginsengSize = STRCHR (buf,'>')-STRCHR (BUF,'<') -1; - } the Else if(STRCHR (BUF,'\"')) + { Apos = STRCHR (buf,'\"') +1; theSize = STRRCHR (buf,'\"')-STRCHR (BUF,'\"') -1; + } - strncpy_s (subname, size, POS, size); $ strcat_s (subpath, SIZE, subname); $ Printheadfile (subpath); -memset (SubName,0, SIZE); -strcpy_s (subpath, SIZE,"D:\\Program Files (x86) \\Microsoft Visual Studio 12.0\\vc\\include\\"); the } - fputs (buf, stdout);Wuyi } the fclose (FP); -}
PS: Note that although some header files are defined in the standard library, they may not exist based on different compilers. So for individual header files, there will be a failure to open the file.
Data structure and algorithm analysis (C language description) Exercise 1.4