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. String formatting question

String formatting question

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomtoolshelp
6 Posts 5 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.
  • N Offline
    N Offline
    Nick Parker
    wrote on last edited by
    #1

    I want to do something like this, however wsprintf throws a compiler error. Is there another format function that I should use instead to do this?

    BSTR path = NULL;
    int num = 1;
    path = SysAllocString(L"//errors/error[@id='%i']");
    wsprintf(path, num);

    - Nick Parker
    My Blog | My Articles

    P C M 3 Replies Last reply
    0
    • N Nick Parker

      I want to do something like this, however wsprintf throws a compiler error. Is there another format function that I should use instead to do this?

      BSTR path = NULL;
      int num = 1;
      path = SysAllocString(L"//errors/error[@id='%i']");
      wsprintf(path, num);

      - Nick Parker
      My Blog | My Articles

      P Offline
      P Offline
      palbano
      wrote on last edited by
      #2

      int wsprintf( LPTSTR lpOut,
      LPCTSTR lpFmt,
      ...
      );

      Nick Parker wrote: BSTR path = NULL; BSTR != LPCTSTR (wrong type)

      int num = 1;
      wsprintf(_T("//errors/error[@id='%i']");, num);

      "No matter where you go, there your are." - Buckaroo Banzai

      -pete

      N 1 Reply Last reply
      0
      • P palbano

        int wsprintf( LPTSTR lpOut,
        LPCTSTR lpFmt,
        ...
        );

        Nick Parker wrote: BSTR path = NULL; BSTR != LPCTSTR (wrong type)

        int num = 1;
        wsprintf(_T("//errors/error[@id='%i']");, num);

        "No matter where you go, there your are." - Buckaroo Banzai

        -pete

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #3

        palbano wrote: BSTR != LPCTSTR (wrong type) I realize that. I need to use a BSTR for the rest of what I am working on. Any other suggestions? - Nick Parker
        My Blog | My Articles

        PJ ArendsP 1 Reply Last reply
        0
        • N Nick Parker

          I want to do something like this, however wsprintf throws a compiler error. Is there another format function that I should use instead to do this?

          BSTR path = NULL;
          int num = 1;
          path = SysAllocString(L"//errors/error[@id='%i']");
          wsprintf(path, num);

          - Nick Parker
          My Blog | My Articles

          C Offline
          C Offline
          cmk
          wrote on last edited by
          #4

          Ummm, there is a problem ... Aside from the BSTR != wchar_t*, what do you expect wsprintf to do ? I mean it's used to take a format string (path) and create a result - you haven't specified a result string. Are you expecting it to modify path in place (it won't even with correct types) ? From MSDN : typedef OLECHAR * BSTR; These strings are zero-terminated, and in most cases they can be treated just like OLECHAR* strings. However, you can query a BSTR for its length rather than scan it, so it can contain embedded null characters. The length is stored as a 32-bit integer at the memory location preceding the data in the string. The 32-bit is placed before the BSTR value so you don't need to worry about it, just cast the BSTR to a wchar_t*. e.g. (without error checking) wchar_t res[500]; BSTR path = NULL; int num = 1; path = SysAllocString(L"//errors/error[@id='%i']"); wsprintf(res, (wchar_t*)path, num); ...cmk Save the whales - collect the whole set

          1 Reply Last reply
          0
          • N Nick Parker

            palbano wrote: BSTR != LPCTSTR (wrong type) I realize that. I need to use a BSTR for the rest of what I am working on. Any other suggestions? - Nick Parker
            My Blog | My Articles

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #5

            Do all formatting using LPTSTRs, then use the T2BSTR conversion macro to get your BSTR type.


            [

            ](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            • N Nick Parker

              I want to do something like this, however wsprintf throws a compiler error. Is there another format function that I should use instead to do this?

              BSTR path = NULL;
              int num = 1;
              path = SysAllocString(L"//errors/error[@id='%i']");
              wsprintf(path, num);

              - Nick Parker
              My Blog | My Articles

              M Offline
              M Offline
              Michael Dunn
              wrote on last edited by
              #6

              You're misusing the BSTR. wsprintf() prints into a TCHAR array, but a BSTR is not an array.

              BSTR path = NULL;
              CString s;
              int num = 1;

              s.Format ( _T("//errors/error[@id='%i']"), num );
              path = s.AllocSysString();

              If you can't/don't want to use CString, alloc a TCHAR buffer instead and make sure it's big enough, then use the T2BSTR conversion macro to convert it to a BSTR. Check out this article[^] if you need more details on converting between string types. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.

              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