Thread: Creating local variables in a program?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    On the Earth.
    Posts
    2

    Creating local variables in a program?

    Hello,

    I want to make a program which reads a file to make multiple automated tasks.
    This file is used to CREATE local variables in a program (like the "INT var =" in C, the "set VAR=" in DOS BATCH, the "VAR=" in SH, etc.). This means the variables name can vary.

    For example (commentary character is #) :
    Code:
    ################################################
    ############## PRINCIPAL SETTINGS ##############
    ################################################
     
    FILEKEY = keylist.cfg
    USER_0 = Lincoln
    USER_1 = Kennedy
    NUMBER_X = 82
    NUMBER_Y = 139
    NUMBER_Z = 226
    INTERVAL = 927
    
    [...]
    My question is : How can I do this? I mean, how can I allocate memory to create local variables used in the program for various tasks?

    If possible, I would like to not use the shell environment i.e. no calls of getenv() or system().
    Also, would it be possible that the names of the variables in the program would be the same that in the file?


    Thank you!

    Sincerely,

    fl0at

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure, if you use a map. But that's not exactly an easy thing to design (and the CRT doesn't have any such thing), so the best thing to do would be switch to C++ and use std::map. Otherwise, design your own map or crawl the web for one.
    Basically, you can combine a key/pair. A key points to a specific variable which can set or lookup. You can look at the tutorial for std::map for more information.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Location
    On the Earth.
    Posts
    2
    Hi,

    First, thank you Elysia for taking the time to answer me clearly.

    After reading your post, I tried to *almost* "crawl the web" to find documentation about maps. The only problem is that the word "map" has several meaning, so, even if i google programming subjects about "maps", I can't find what I am searching for.

    I was wondering if anyone could explicate me what is a map, or if anyone could point me to a decent documentation.
    I do not project to use a map from someone else than me, but I would like to understand what is the principle of a map.
    I am sure I can code one myself :-]


    Unfortunately, for the moment, I am concentrated on C, and I don't know well C++ :-[

    Thank you,

    Sincerely,

    fl0at

  4. #4

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The more generic term is "associative array". Wikipedia has a short article, and there are others out there as well. (You'll probably get a lot of perl matches, since it's a built-in data type in that language.) You can search on the full C++ name "std::map" as well.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    std::map is a C++ container.
    Since you're using C, you'll have to reinvent the wheel the hard way.

    For your purposes, a linked list of structs like this might be appropriate:
    Code:
    enum var_type
    {
        INT,
        STRING,
        DOUBLE
    }
    
    struct Variable
    {
        var_type  varType;
        char*  varName;
        void*  varData;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-12-2009, 06:14 PM
  2. Replies: 4
    Last Post: 03-16-2009, 08:44 AM
  3. Creating several(400) data files within a program
    By Isolda_ in forum C Programming
    Replies: 11
    Last Post: 09-11-2007, 11:57 AM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Creating a file with a program
    By RazielX in forum C Programming
    Replies: 7
    Last Post: 03-12-2004, 10:06 AM