Click to See Complete Forum and Search --> : Dynamic formating a string


Vincent Harink
May 4th, 1999, 03:29 AM
Wat I want is to read a text string from a (configuration) file,
e.g. "This is line number %d, used for %s.,inumber, strtext"
In my program i have also defined the variables:
int inumber = 12;
CString strtext = "testing"
ect.

How can I read this string an convert it to the formated string I want ?

This is line number 12, used for testing

The meaning is that I can define various formatted strings in a text
file witch I can use in my program.

VHARINK@DAXIS.NL

Dave Lorde
May 4th, 1999, 08:47 AM
Use the CString::Format() method:

int inumber = 12;
CString strtext = "testing";
CString formatStr = "This is line number %d, used for %s.";
CString outString;
outString.Format(formatStr, inumber, strtext);

Dave

Vincent Harink
May 4th, 1999, 01:35 PM
At compilation time I don't know that I have to use the variables inumber and strtext.
Depending on the variables that are in the configuration file I have to use the variables that are in my program with the same name.
So I can't write:
outString.Format(formatStr, inumber, strtext);
in my source code because I don't know at that time I have to use inumber and strtext .

What can I do ?

Paul McKenzie
May 4th, 1999, 02:32 PM
In the configuration file, how do you distinguish between the format string and the list of variables? From looking at your example, the whole line in the configuration file is double quoted.

Example:
"This is line number %d, used for %s.,inumber, strtext"

I would have expected something like this:
"This is line number %d, used for %s", "inumber", "strtext"

Next, what you explained to Dave Lorde in the other post is *not* as trivial as it sounds. You are trying to associate a string name of a variable to an actual variable name in your program. Java has the ability to do this, but not C++ (at least not natively).

This can be done various ways, but each has its advantages and disadvantages. One way is to build a map that associates the name from the file to the actual variable in your program. The map would essentially be string-->pointer, where pointer is the pointer to the variable represented by the string. The advantage to this is that all you need to do is lookup the string name and you can find the pointer to the variable. The disadvantage is that you would need to do a cast to make sure that the pointer is casted to the correct type when you are creating the formatted string (since you can only store pointers of only one type in the map).

Regards,

Paul McKenzie

Dave Lorde
May 5th, 1999, 04:20 AM
OK, I see what you mean now.

As Paul says, what you want to do is not really supported by the language at runtime, though there are ways of achieving something with similar effect.

If you have control of the configuration file, I'd suggest that, instead of using program variable names directly, you use coded strings or numerics from a pre-determined table that can be interpreted by your program and used to select the appropriate variables.

Dave

Vincent Harink
May 5th, 1999, 04:51 AM
I think this is a good sollution.
I have done some tests with casting pointers, but it did not work fine.
Here is an example to explain what I mean.

int ITest = 1234;
long lTest = 456;
double dTest = 1234.5678;
CString strTest = "abcd";

void* tp[10];
CString strOutput;

tp[1] = (void*) dts;
tp[2] = (void*) toolnr;
tp[3] = (void*) lts;
tp[4] = (void*) vjm;

Test.Format("Double: %11.3f, Integer: %-16d, Long: %ld , String: %s", tp[1],tp[2],tp[3],tp[4]);

Can someone help me to cast this pointers on a good way.
An example would be verry helpfull

Paul McKenzie
May 5th, 1999, 10:37 AM
Another suggestion is to use C++ streams (strstreambuf) instead of Format. There would be no need to cast if you used streamed output. You can also use the iomanip functions to format the data.

Regards,

Paul McKenzie

Shawn Fessenden
May 5th, 1999, 02:29 PM
Wat you need is FormatMessage. This is wrapped as a member of CString also. Check the dox carefully, it does exactly what you want (and more). It can also be extended (via multiple, nested calls) to handle just about any situation.

"Geek used to be a four-letter word. Now it's a six-figure one."