C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-02-2008, 04:01 PM   #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
fl0at is offline   Reply With Quote
Old 01-02-2008, 04:13 PM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 01-04-2008, 06:39 PM   #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
fl0at is offline   Reply With Quote
Old 01-04-2008, 06:44 PM   #4
Registered User
 
Join Date: Oct 2001
Posts: 2,110
http://www.google.com/search?q=map+STL
robwhit is offline   Reply With Quote
Old 01-04-2008, 06:45 PM   #5
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,825
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.
tabstop is online now   Reply With Quote
Old 01-04-2008, 07:34 PM   #6
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,282
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;
};
cpjust is online now   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simplest question ever! Sorry i can't work it out...!- Local variables JezW C Programming 9 04-12-2009 06:14 PM
problem comparing variables from my main program into my subprogram Raschoc C++ Programming 4 03-16-2009 08:44 AM
Creating several(400) data files within a program Isolda_ C Programming 11 09-11-2007 11:57 AM
BOOKKEEPING PROGRAM, need help! yabud C Programming 3 11-16-2006 11:17 PM
Creating a file with a program RazielX C Programming 7 03-12-2004 10:06 AM


All times are GMT -6. The time now is 09:44 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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