C/Reading variable names from a file.
Expert: Zlatko - 4/27/2010
QuestionHi
I would like to ask if it possible to have a text file that contains MACRO names, and while reading the file call the MACRO and print its value.
Example: text file contains the following
SUNDAY
THURSDAY
FRIDAY
in my C file has the following MACROs:
#define SUNDAY "FirstDay"
#define MONDAY "SecondDay"
#define THURSDAY "ThirdDay"
.
.
.
#define FRIDAY "SixthDay"
then get the C problem to read the file and print":
FirstDay
ThirdDay
SixthDay
It is important to note that I can not use switch case / if statements for compare the file input with strings.
Maybe some macro that could read the file's input and call the relevant macro.
Thanks in advance
AnswerHello Islam
I am quite sure that this cannot be done. A macro is not "called" at runtime. A macro is expanded by the pre-processor to generate C code before the actual compiler is called. Therefore, if a programmer cannot write the desired code in C, then it cannot be done by macro either. A programmer cannot read in unknown text at runtime and take an action based on its value without first checking its value.
If it turns out that I am wrong about all this, and you do find a way, please be kind enough to let me know.
Best regards
Zlatko