read a constant from a file

This is a discussion on read a constant from a file within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying to read a settings file for a program i'm working on. This settings file is something ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    5

    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:

    option1=asdf
    option2=5
    option3=CONSTANT_X

    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

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

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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
    Posts
    5
    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
    Posts
    5
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:01 PM
  2. File read in problem
    By DimensionX in forum C Programming
    Replies: 5
    Last Post: 12-02-2009, 11:37 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04: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, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21