简体   繁体   中英

Preprocessor C program to replace include headers

I have homework assignment where I need to write a program to replace include commands (assuming there are no standard library includes) of C file with the actual.h' file (performing the preprocessor's job).

I thought using regex matching, but since we are only allowed to use ansi-C's standard libraries regex is not allowed.

I am not sure what is the efficient way to do so?

edit: there is no need to check for #defines or #ifdefs .

I suggest you follow this guide to create your program:

  1. Open the target c file read only.
  2. Open the destination file write only.
  3. Create a function that you pass the input file and the output file, i will call this function "expand_function".
  4. In the expand_function read each byte of the input, and while reading each character check if it is a #, then test if it is a include keyword, and finally if all conditions matches open the filename that will be in front the include, then call again the expand_function with the new input file and the same output file (don't forget closing all open files), and finally continue the loop. If any conditions not match write all read bytes in the output file.
  5. Extra: Check if you are not in a dead loop of includes in the beginning of expand_function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM