Data structure and algorithm analysis (C language description) Exercise 1.4

Source: Internet
Author: User

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 (&AMP;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.