Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. vsprintf can not work on mac OS
QtWS25 Last Chance

vsprintf can not work on mac OS

Scheduled Pinned Locked Moved Unsolved C++ Gurus
vsprintfmac os
4 Posts 2 Posters 2.2k Views
  • 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
    shangke
    wrote on 4 Jun 2017, 13:16 last edited by
    #1

    On mac OS, the function vsprintf can't print string with parameter %s or %ls ,
    but it works well on Windows.
    I have tried on Qt5.8 and Qt5.9.

    Please help me, thanks.
    code:

    #include <QCoreApplication>

    #include <string>
    using namespace std;

    wstring FormatStr( const wchar_t* wszFormat, ... )
    {
    va_list ptr;
    va_start(ptr, wszFormat);

    int nLen = vwprintf(wszFormat, ptr) + 1;
    
    wchar_t* lpDest = new wchar_t[nLen];
    
    vswprintf(lpDest, nLen, wszFormat, ptr);
    va_end(ptr);
    
    wstring strRe = lpDest;
    
    delete lpDest;
    return strRe;
    

    }

    string FormatStrA( const char* szFormat, ... )
    {
    va_list ptr;
    va_start(ptr, szFormat);
    int nLen = vprintf(szFormat, ptr) + 1;

    char* lpDest = new char[nLen];
    
    vsprintf( lpDest, szFormat, ptr );
    va_end(ptr);
    
    string strRe = lpDest;
    
    delete lpDest;
    return strRe;
    

    }

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    wstring str = FormatStr( L"%ls/%d/%d/%ls", L"https://www.google.com", 2, 3, L"asdfasdfasd");
    string str1 = FormatStrA( "%s/%d/%d/%s", "https://www.google.com", 2, 3, "asdfasdfasd");

    // On windows: str == str1  == "https://www.google.com/2/3/asdfasdfasd"
    // On Mac: str == str1 == (null)/2/3/(null)
    return a.exec();
    

    }

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on 5 Jun 2017, 12:51 last edited by mpergand 6 May 2017, 12:54
      #2

      You use vprintf two times for the same valist ...

      #include <string>
      #include <cwchar>
      #include <iostream>
      using namespace std;
      
      wstring FormatStr( const wchar_t* wszFormat, ... )
      {
      va_list ptr;
      va_start(ptr, wszFormat);
      int nLen = vwprintf(wszFormat, ptr) + 1;
      va_end(ptr);
      
      std::cout<<" (1)"<<std::endl;
      wchar_t* lpDest = new wchar_t[nLen];
      
      va_start(ptr, wszFormat);
      vswprintf(lpDest, nLen, wszFormat, ptr);
      va_end(ptr);
      
      wstring strRe = lpDest;
      
      delete lpDest;
      return strRe;
      }
      
      string FormatStrA( const char* szFormat, ... )
      {
      va_list ptr;
      va_start(ptr, szFormat);
      int nLen = vprintf(szFormat, ptr) + 1;
      va_end(ptr);
      
      char* lpDest = new char[nLen];
      std::cout<<" (2)"<<std::endl;
      
      va_start(ptr, szFormat);
      vsprintf( lpDest, szFormat, ptr );
      va_end(ptr);
      
      string strRe = lpDest;
      
      delete lpDest;
      return strRe;
      }
      
      
      
      
      int main(int argc, char *argv[])
      {
      wstring str = FormatStr( L"%ls/%d/%d/%ls", L"https://www.google.com", 2, 3, L"asdfasdfasd");
      string str1 = FormatStrA( "%s/%d/%d/%s", "https://www.google.com", 2, 3, "asdfasdfasd");
      std::wcout<<str<<" (3)"<<std::endl;
      std::cout<<str1<<" (4)"<<std::endl;
      

      logs:

      https://www.google.com/2/3/asdfasdfasd (1)
      https://www.google.com/2/3/asdfasdfasd (2)
      https://www.google.com/2/3/asdfasdfasd (3)
      https://www.google.com/2/3/asdfasdfasd (4)
      

      on Mac OSX 10.9.5

      1 Reply Last reply
      1
      • S Offline
        S Offline
        shangke
        wrote on 5 Jun 2017, 15:14 last edited by
        #3

        Thank you very much!
        It does work on mac OS.
        But,
        I found that the function FormatStrA crashed on ubuntu16.
        Please help me one more time, thank you.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on 5 Jun 2017, 21:31 last edited by mpergand 6 May 2017, 21:32
          #4

          What does the debugger say ?
          It can crash in many ways.

          Maybe you could use a large static buffer:
          char dest[1000];
          and use vsnprintf, vswprintf instead.

          1 Reply Last reply
          0

          4/4

          5 Jun 2017, 21:31

          • Login

          • Login or register to search.
          4 out of 4
          • First post
            4/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved