June 16th, 2003, 04:47 PM
C, sprintf - Displaying percent signs
I'm currently using sprintf's to create query strings for database calls. Using a MySQL format command, I need to keep quite a few percent signs literal for it to do it's job. Upon reading I found that using '%%' in the string should create literal percent signs, however it's not working in my percent-filled string. Here's my code, followed by an example of the actual string after the sprintf call.
sprintf( query_string, "SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(%lu,'%%Y%%m%%d%%H0000'))", otop );
Thanks in advance
June 16th, 2003, 05:59 PM
I'm afraid this isn't going to help you much. I copied and pasted your code in to a test program and it worked for me.
Here's a "screen dump" showing the program and its output. I compiled with MinGW's gcc:
unsigned long otop = 1041404400L;
"SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(%lu,'%%Y%%m%%d%%H0000'))", otop );
"sprint.c" 21 lines, 394 characters
June 16th, 2003, 06:05 PM
Huh... well I ended up taking another route to find the information I needed, but thanks for the post. I'm not sure if I should be afraid of a memory leak somewhere or not. I'd say yes because what has happened is odd, but the fact that it was so consistantly wrong (very same output every time) I'm hesitantly ruling out a memory leak.
Well... we'll see if my program blows up I guess. :)
June 16th, 2003, 07:17 PM
If your compiler was interpreting some of those double-%'s as format specifiers, then the weird results would be a symptom of having more format specifiers than variables.
sprintf has an array of variables from which to get the values that it is going to format, so it just keeps on going whether you had put anything there or not. In the case where you give it one variable but there are three format specifiers, then the first variable value will go in as expected and then for the second and third it will be reading garbage and you get really wild results. So if that happens, you need to check that you have provided one variable for each format specifier.
In this case, it shouldn't have happened, but for some reason it looks like your compiler misinterpreted what you wrote. Could you tell us what your compiler is?
June 17th, 2003, 12:41 PM
I'm compiling with gcc.
Considering the output I was getting, it seems as though it was recognizing %%m and %%d as format specifiers, since the leading percent wasn't kept-- but then it might exclude that for having no recognized completion.
June 17th, 2003, 02:11 PM
-actually what happens is pretty cool. if you do something like this:
int x = 8; int *ptr = &x;
printf("x address is: %p\n", ptr);
printf("stack dump: %08x : %08x : %08x\n");
x address is: 0xbffffb04
stack dump: bffffb04 : bffffb08 : 08048246
you can rip values right off of the stack and get a dump of the stack memory:D
June 17th, 2003, 02:19 PM
My previous test was run on Windows 98 using the MinGW port of gcc. I just compiled and ran the exact same program on Red Hat 7.0 using gcc version 2.96 20000731 (Red Hat Linux 7.0). I got the same results; it still works for me.
Just as a wild idea, copy my program from the previous post and compile and run it. See if you get the same results as I do. I'm not getting my hopes up, because I had copied and pasted your sprintf statement from your post, but it still might be worth a try. At least it couldn't hurt ... much.
Last edited by dwise1_aol; June 17th, 2003 at 02:22 PM.
June 17th, 2003, 02:35 PM
I would say to try dwise1_aol's example program also. If it works and yours doesn't, you might want to check to see if enough size was allocated to the variables that you are using.