DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   how to insert variables into string with % sign? (http://www.daniweb.com/forums/thread108931.html)

linux0id Feb 13th, 2008 3:36 am
how to insert variables into string with % sign?
 
Hello everyone!
I have seen some code where some variable gets inserted into string throught the % sign, like so -
("insert into songs values (%Q ,%d, %d, %Q, %Q, %Q, ... ", someInt, someChar, someChar... )
I have tried to look it up in google but I do not know what this is called. What is it and where can I find a reference for it? Thanks!

niek_e Feb 13th, 2008 3:48 am
Re: how to insert variables into string with % sign?
 
Quote:

("insert into songs values (%Q ,%d, %d, %Q, %Q, %Q, ... ", someInt, someChar, someChar... )

Do you use someting like sprintf_s()? (or sprintf())
example:
char c = 'x';
int i = 123;
char buffer[1024];
sprintf(buffer, "Test number: %d, test char, %c", i,c);

linux0id Feb 13th, 2008 3:52 am
Re: how to insert variables into string with % sign?
 
No, but I want to use this for sqlite3 to insert data into tables, like combine the "insert into" command with data. Do I have to "print" it to string and then use it? Thanks

vijayan121 Feb 13th, 2008 5:35 am
Re: how to insert variables into string with % sign?
 
> I have seen some code where some variable gets inserted into string through the % sign
the code you saw was probably using the Boost.Format library to insert stuff into a stringstream. http://www.boost.org/libs/format/doc/format.html

WaltP Feb 13th, 2008 12:39 pm
Re: how to insert variables into string with % sign?
 
The string with the %x symbols in it are parsed to find each symbol. The letter is then looked at to figure out what field is supposed to go there. Then the string is changed by removing the %x and whatever field is requested replaces it.

Usually the data comes out of variables and/or structures the program has already read or generated.

There is no function to do this if that's what you're looking for. The function is written by the programmers developing the system.

linux0id Feb 13th, 2008 1:25 pm
Re: how to insert variables into string with % sign?
 
its not the boost library, I had a look again - it is the sprintf function. Now I will look at the documentation.


All times are GMT -4. The time now is 3:42 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC