Results 1 to 7 of 7

Thread: Problem with swprintf function

  1. #1
    Join Date
    Aug 2006
    Posts
    83

    Default Problem with swprintf function

    Hi,

    I'm having troubles with using swprintf function. I'm trying to concatenate std::string with wide string and an integer like this:

    wchar_t buffer[128];
    swprintf( buffer, 128, L" Hello %s %d", /* std::string */ name , /* int */ number);

    The problem is in std::string, because when I try to write the buffer to the screen it always tells me that the name is [null] -> but it isn't. Can anyone tell me what I'm doing wrong ?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with swprintf function

    Try name.c_str() instead of name.

  3. #3
    Join Date
    Aug 2006
    Posts
    83

    Default Re: Problem with swprintf function

    I already tried it, but it doesn't do the trick for me. Instead of concatenating actual string like "michael" it concatenates characters like [] <- boxes....

    Any ideas ?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with swprintf function

    What does the std::string contain (I mean, what encoding does it use)?

  5. #5
    Join Date
    Aug 2006
    Posts
    83

    Default Re: Problem with swprintf function

    That's a good question. I read the string from the .xml file (which uses utf-8 encoding) so I guess the std::string should use the same (utf-8) encoding ?? Correct me if I'm wrong please.
    Last edited by moowy; 8th May 2007 at 15:54.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem with swprintf function

    I don't know if you're wrong but C strings (char* and possibly std::string) can handle strings that contain 0x00 characters and Unicode (and possibly UTF-8) encoding uses them. So I suggest not using std::string at all (in favour of unicode capable counterparts like wstring or QString).

  7. #7
    Join Date
    Aug 2006
    Posts
    83

    Default Re: Problem with swprintf function

    I switched to std::wstring-s and I have no problems with them and swprintf function. It works like charm . Tnx for your help

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  3. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  5. K&R's strcpy function - problem with pointers
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 8th January 2006, 15:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.