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. sprintf_s problem

sprintf_s problem

Scheduled Pinned Locked Moved C / C++ / MFC
databasehelpquestion
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.
  • T Offline
    T Offline
    tataxin
    wrote on last edited by
    #1

    hi, I have a sql in my program. What I want is exactly like this:

    select *
    from tbl t
    where t.name like '%xyz%'

    So in source code, I create sql like this:

    CString name = "xyz";
    ::sprintf_s(szQuery, " select * from tbl t where t.name like '%%%s%%' ", name);

    Here is szQuery when I execute

    select *
    from tbl t
    where t.name like '%x%'

    Just the first character is printed into szQuery. What's wrong here? what should I do?? Thank you in advance.

    C N 2 Replies Last reply
    0
    • T tataxin

      hi, I have a sql in my program. What I want is exactly like this:

      select *
      from tbl t
      where t.name like '%xyz%'

      So in source code, I create sql like this:

      CString name = "xyz";
      ::sprintf_s(szQuery, " select * from tbl t where t.name like '%%%s%%' ", name);

      Here is szQuery when I execute

      select *
      from tbl t
      where t.name like '%x%'

      Just the first character is printed into szQuery. What's wrong here? what should I do?? Thank you in advance.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Do you have UNICODE enabled ?

      Cédric Moonen Software developer
      Charting control [v1.4] OpenGL game tutorial in C++

      T 2 Replies Last reply
      0
      • C Cedric Moonen

        Do you have UNICODE enabled ?

        Cédric Moonen Software developer
        Charting control [v1.4] OpenGL game tutorial in C++

        T Offline
        T Offline
        tataxin
        wrote on last edited by
        #3

        yes, Cedric. It's a Unicode project. In Project properties, The Character Set is "Use Unicode Character Set" is that wrong?

        C 1 Reply Last reply
        0
        • C Cedric Moonen

          Do you have UNICODE enabled ?

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++

          T Offline
          T Offline
          tataxin
          wrote on last edited by
          #4

          Thank you, Cedric. I just change the Character Set property to "Use Multi-Byte Character Set" and it's ok now

          modified on Thursday, July 17, 2008 9:40

          1 Reply Last reply
          0
          • T tataxin

            hi, I have a sql in my program. What I want is exactly like this:

            select *
            from tbl t
            where t.name like '%xyz%'

            So in source code, I create sql like this:

            CString name = "xyz";
            ::sprintf_s(szQuery, " select * from tbl t where t.name like '%%%s%%' ", name);

            Here is szQuery when I execute

            select *
            from tbl t
            where t.name like '%x%'

            Just the first character is printed into szQuery. What's wrong here? what should I do?? Thank you in advance.

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #5

            I think your project is a unicode project. So the CString will be holding a pointer to unicode string. So you the following options. 1. use _stprintf_s() intead of sprintf_s( in this case szQuery also need to be TCHAR* or TCHAR[] ) or 2. Use CStringA instead of CString or 3. Convert CString content to multibyte characters using WideCharToMultiByte() API. 4. Undef _UNICODE.

            nave [OpenedFileFinder]

            C T 2 Replies Last reply
            0
            • T tataxin

              yes, Cedric. It's a Unicode project. In Project properties, The Character Set is "Use Unicode Character Set" is that wrong?

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              Then you have to specify %S instead of %s. CString will return a wide-character string since UNICODE is enabled. If you look at the format specification[^] you can see that %S tells printf that the string is a wide-char string (when used with printf alike functions).

              Cédric Moonen Software developer
              Charting control [v1.4] OpenGL game tutorial in C++

              1 Reply Last reply
              0
              • N Naveen

                I think your project is a unicode project. So the CString will be holding a pointer to unicode string. So you the following options. 1. use _stprintf_s() intead of sprintf_s( in this case szQuery also need to be TCHAR* or TCHAR[] ) or 2. Use CStringA instead of CString or 3. Convert CString content to multibyte characters using WideCharToMultiByte() API. 4. Undef _UNICODE.

                nave [OpenedFileFinder]

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                5. Or simply use %S instead of %s :-D

                Cédric Moonen Software developer
                Charting control [v1.4] OpenGL game tutorial in C++

                T 1 Reply Last reply
                0
                • C Cedric Moonen

                  5. Or simply use %S instead of %s :-D

                  Cédric Moonen Software developer
                  Charting control [v1.4] OpenGL game tutorial in C++

                  T Offline
                  T Offline
                  tataxin
                  wrote on last edited by
                  #8

                  nice!! If we use "Use Multi-Byte Character Set", it should be "%s". Or if we use "Use Unicode Character Set", it should be "%S". Thank you, Cedric!!

                  1 Reply Last reply
                  0
                  • N Naveen

                    I think your project is a unicode project. So the CString will be holding a pointer to unicode string. So you the following options. 1. use _stprintf_s() intead of sprintf_s( in this case szQuery also need to be TCHAR* or TCHAR[] ) or 2. Use CStringA instead of CString or 3. Convert CString content to multibyte characters using WideCharToMultiByte() API. 4. Undef _UNICODE.

                    nave [OpenedFileFinder]

                    T Offline
                    T Offline
                    tataxin
                    wrote on last edited by
                    #9

                    I will try them now. At least the option 2 works There 're many functions similar to each other. It's so messy, and not easy to understand .... :( Thank you, Naveen.

                    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