BugRepairMan BugRepairMan - 1 year ago 72
Python Question

Add header #ifndef into C header files in current directory

In current directory, All C header files (*.h) don't include the preprocessor macro

#ifndef FILENAME_H
#define FILENAME_H

, it's too tedious to add them in each header manually. How to do that automatically via python or shell?

Answer Source

I'm going to second @kaylum's advice and also not give you the whole thing, but here is some pseudocode that might set you on the right path

for each file in the directory
   if filename doesn't end with .h
   open the file
   store its contents in a variable
   create the header guard by taking the filename, removing the '.', and replacing it with a '_'
   create new contents = headerGuard + contents + "\n#endif"
   write file back out to the same name

Each of these things should be answerable with a quick google/stack overflow search, and if you can't figure out any of those parts, a specific stack overflow question about that bit would be better suited for this site. And here is one link to a relevant question to get you started.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download