printf question
-
I am converting a UNIX application to Windows using VC++ and MFC. There are a large number of printf statements in the code, so I re-defined the printf function, with vsprintf, so that all text will go to a richedit control. The problem I face is that when a data file is read, it is possible that one of the fields will have a "%" sign as part of its value. When this value is displayed, it looks as though vsprintf is behaving as documented and printing the value without the % sign. Does anyone know of an easy, efficient way to make sure the % sign is printed as well? Thanks, Shawn
-
I am converting a UNIX application to Windows using VC++ and MFC. There are a large number of printf statements in the code, so I re-defined the printf function, with vsprintf, so that all text will go to a richedit control. The problem I face is that when a data file is read, it is possible that one of the fields will have a "%" sign as part of its value. When this value is displayed, it looks as though vsprintf is behaving as documented and printing the value without the % sign. Does anyone know of an easy, efficient way to make sure the % sign is printed as well? Thanks, Shawn
Shawn Horton wrote: Does anyone know of an easy, efficient way to make sure the % sign is printed as well? I'm not sure what exactly do you want to achieve. You've mentioned that there are fields in data file and vsprintf replaces % sequences with actual values. Do you want to keep a percent sign before the value? If this is the case, try appending %% in front - this will result in one percent sign being printed. Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
-
Shawn Horton wrote: Does anyone know of an easy, efficient way to make sure the % sign is printed as well? I'm not sure what exactly do you want to achieve. You've mentioned that there are fields in data file and vsprintf replaces % sequences with actual values. Do you want to keep a percent sign before the value? If this is the case, try appending %% in front - this will result in one percent sign being printed. Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
Maybe this will help: char data_name[DATANAMELEN]; read_function(data_name); //data_name now contains the value "%recovered" printf("we read in %s", data_name); The printf statement to the Richedit window is now we read in recovered notice the missing %? Maybe that is a bit clearer. My hope is that I will not have to look for a % in character in each variable, and then insert a % in front of that. Thanks for the help.
-
Maybe this will help: char data_name[DATANAMELEN]; read_function(data_name); //data_name now contains the value "%recovered" printf("we read in %s", data_name); The printf statement to the Richedit window is now we read in recovered notice the missing %? Maybe that is a bit clearer. My hope is that I will not have to look for a % in character in each variable, and then insert a % in front of that. Thanks for the help.
Shawn Horton wrote: Maybe that is a bit clearer. Totally clear now. I don't know about any format modifier which could cause [v]sprintf to literally copy % sign. Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
-
Shawn Horton wrote: Maybe that is a bit clearer. Totally clear now. I don't know about any format modifier which could cause [v]sprintf to literally copy % sign. Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
I should just post something like "Duh I am having a problem" on my first post, because the second one usually makes more sense.
-
I should just post something like "Duh I am having a problem" on my first post, because the second one usually makes more sense.
If you're using MFC in your project, use CString::Format - it will work for you. Tomasz Sowinski -- http://www.shooltz.com
What is "scratch" and why can everything be made from it?
-
Maybe this will help: char data_name[DATANAMELEN]; read_function(data_name); //data_name now contains the value "%recovered" printf("we read in %s", data_name); The printf statement to the Richedit window is now we read in recovered notice the missing %? Maybe that is a bit clearer. My hope is that I will not have to look for a % in character in each variable, and then insert a % in front of that. Thanks for the help.
Sounds like a bug in vsprintf :-( It surely should take everything that is passed literally as literals. Or I am missing something here? Maybe it is a feature, called somewhat like "recursive format substitution"... What happens if you replace "%" with "%%" in your data_name? -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )
-
Sounds like a bug in vsprintf :-( It surely should take everything that is passed literally as literals. Or I am missing something here? Maybe it is a feature, called somewhat like "recursive format substitution"... What happens if you replace "%" with "%%" in your data_name? -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )
Actually, it was a bug in our re-defined printf. The function for the richedit control that we were using was expecting both a format specifier and a va_list, but we forgot the format specifier. Thus, when the function saw %recovered, it saw an invalid format specifier (%r) and threw out the % like it said it would. I hate when compilers do what you tell them to do instead of doing what you want them to do. Thanks for the help. Shawn
-
Actually, it was a bug in our re-defined printf. The function for the richedit control that we were using was expecting both a format specifier and a va_list, but we forgot the format specifier. Thus, when the function saw %recovered, it saw an invalid format specifier (%r) and threw out the % like it said it would. I hate when compilers do what you tell them to do instead of doing what you want them to do. Thanks for the help. Shawn
Shawn Horton wrote: I hate when compilers do what you tell them to do instead of doing what you want them to do. Yeah... After 40 years of science, computers are still stupid at all. Tons of work remaining for us developer folks ;P ;) -- Daniel Lohmann http://www.losoft.de (Hey, this page is worth looking! You can find some free and handy NT tools there :-D )