You are here:

C/Reading variable names from a file.

Advertisement


Question
Hi

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

Answer
Hello 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.