![]() |
| | #1 |
| Registered User Join Date: Jan 2008 Location: On the Earth.
Posts: 2
| Creating local variables in a program? 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 [...] 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 | |
| | #2 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #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 | |
| | #4 |
| Registered User Join Date: Oct 2001
Posts: 2,110
| |
| robwhit is offline | |
| | #5 |
| and the Hat of Guessing 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |