Thread: read a constant from a file

  1. #1
    Registered User
    Join Date
    Jun 2010

    read a constant name from a file

    Hi everyone,

    I'm trying to read a settings file for a program i'm working on. This settings file is something like this:


    where CONSTANT_X is a string too.

    There is no problem about reading the numbers or strings from the program but, how can i read CONSTANT_X and use it as a constant in my program?
    what i mean is to use it as:

    int number=CONSTANT_X; (for example)

    how can i interpret the constant from a string?? I don't know if i'm explaining it right

    Last edited by Kais3r; 06-24-2010 at 08:33 AM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    So get the value and put it in a const variable that is declared but not initialized until you initialize it with that value.

    To get the value use strtol() assuming it's an integer value.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2010
    no, you are not getting the value from the file, you are getting the name of the constant, and you want to use it.

    Imagine you would have CONSTANT_X defined in your whatever_you_are_doing and you are preparing a demo or something, and you need to use some of this constants that you have defined in your program in that file.

    I'm doing some stuff with a DDS distribution, a communication protocol, and it has a lot of predefined constants. What i want to do is some kind of settings for one kind of entity and some of this parameters must be initialized with constants, so in the settings file i must use the constant's name.

    Imagine a Reliability parameter, it can be defined as "REALIABLE" or "BEST_EFFORT" so i need my program to read the "RELIABLE" string or the "BEST_EFFORT" string and be able of interpret them as the predefined constant

    I hope i have explained myself better

  4. #4
    Registered User
    Join Date
    Jun 2010
    well, the post is not needed any more it can be closed

    The answer is that reflection is not implemented in C, so it can't be done that way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  2. File read in problem
    By DimensionX in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 12:37 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM