Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. printf question

printf question

Scheduled Pinned Locked Moved C / C++ / MFC
c++questionhelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shawn Horton
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • S Shawn Horton

      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

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      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?

      S 1 Reply Last reply
      0
      • T Tomasz Sowinski

        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?

        S Offline
        S Offline
        Shawn Horton
        wrote on last edited by
        #3

        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.

        T D 2 Replies Last reply
        0
        • S Shawn Horton

          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.

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          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?

          S 1 Reply Last reply
          0
          • T Tomasz Sowinski

            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?

            S Offline
            S Offline
            Shawn Horton
            wrote on last edited by
            #5

            I should just post something like "Duh I am having a problem" on my first post, because the second one usually makes more sense.

            T 1 Reply Last reply
            0
            • S Shawn Horton

              I should just post something like "Duh I am having a problem" on my first post, because the second one usually makes more sense.

              T Offline
              T Offline
              Tomasz Sowinski
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              • S Shawn Horton

                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.

                D Offline
                D Offline
                Daniel Lohmann
                wrote on last edited by
                #7

                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 )

                S 1 Reply Last reply
                0
                • D Daniel Lohmann

                  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 )

                  S Offline
                  S Offline
                  Shawn Horton
                  wrote on last edited by
                  #8

                  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

                  D 1 Reply Last reply
                  0
                  • S Shawn Horton

                    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

                    D Offline
                    D Offline
                    Daniel Lohmann
                    wrote on last edited by
                    #9

                    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 )

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups