473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

stringize variable name

Hi

I have n bool variables in a namespace, I need to write a routine
that sets the value of each according to whats in n corresponding files
named after the variables.
#include <vector>
#include <string>
using namespace std;
void set_switch(bool& swi, const string& f){
ifstream in( f.c_str() );
string line;
getline(in, line);
if( line == "false" )
swi = false;
else
swi = true;
}
int main(){
bool abc, def;
set_switch(abc, "status/abc.txt");
set_switch(def, "status/def.txt");
// ... n times
}

but I want to avoid writing this n times. I thought to place the bool
variables in a vector<booland use a stringizer or even the processor
macro # but could not get an elegant solution.
I want to take advantage of the fact that all the files names are the
same pattern with the bool identifiers.

could some one help please.

thanks

Nov 8 '06 #1
1 3387
Gary Wessle <ph****@yahoo.comwrites:
Hi

I have n bool variables in a namespace, I need to write a routine
that sets the value of each according to whats in n corresponding files
named after the variables.
#include <vector>
#include <string>
using namespace std;
void set_switch(bool& swi, const string& f){
ifstream in( f.c_str() );
string line;
getline(in, line);
if( line == "false" )
swi = false;
else
swi = true;
}
int main(){
bool abc, def;
set_switch(abc, "status/abc.txt");
set_switch(def, "status/def.txt");
// ... n times
}
#v+
#define SET_SWITCH(var) set_switch(var, "status/" ##var ".txt")
int main() {
bool abc, def;
SET_SWITCH(abc);
SET_SWITCH(def);
return 0;
}
#v-

That's as far as you can get with a preprocessor I think. The other
method would be:

#v+
int main() {
static const char *file_names[] = { "abc", "def" };
char buffer[128] = "status/";
std::vector<boolvec;
for (unsigned i = 0; i<sizeof file_names / sizeof *file_names; ++i) {
bool tmp;
strncat(buffer, file_names[i], sizeof buffer - 1);
strncat(buffer, ".txt", sizeof buffer - 1);
set_switch(tmp, buffer);
vec.push_back(tmp);
}
return 0;
}
#v-

Or you could use some kind of std::map<string, boolif you want to
have named flags, eg. something like:

#v+
int main() {
static const char *flag_names[] = { "abc", "def" };
char buffer[128] = "status/";
std::map<std::string, boolflags;
for (unsigned i = 0; i<sizeof flag_names / sizeof *flag_names; ++i) {
bool tmp;
strncat(buffer, flag_names[i], sizeof buffer - 1);
strncat(buffer, ".txt", sizeof buffer - 1);
set_switch(tmp, buffer);
flags[std::string(flag_names[i])] = tmp;
}
return 0;
}
#v-

(note that none of the code was tested)

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Nov 8 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
2
by: Bradford | last post by:
Question for the masses... Lets say I have variable with the following contents "aaaa bbbb ccccc dddd". The format is not specific and the space delimiter could be changed to any other. How...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for banner </xsl:variable> <xsl:variable...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: <xsl:variable...
3
by: travish | last post by:
Is the stringize operator for macros commonly/correctly implemented on compilers advertising ANSI C compatibility? Example: #define STRINGIZE(ARG) ARG = #ARG void main() { char *test;
2
by: Kevin | last post by:
I am having difficulty updating a variable page-time-stamp in the following snippit. The variable time-stamp is initialized from the attribute time-stamp from the log element. Some of the page...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.