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. Snprintf usage?
Servers for Qt installer are currently down

Snprintf usage?

Scheduled Pinned Locked Moved C++ Gurus
10 Posts 7 Posters 7.1k Views 2 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.
  • P Offline
    P Offline
    Psycho_Path
    wrote on 8 Mar 2012, 02:46 last edited by
    #1

    Trying to use snprintf but -QT- Qt isn't recognizing the function. I've included <stdio.h> but I'm using namespace std; due to using vectors. Any help here would be appreciated, thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on 8 Mar 2012, 03:12 last edited by
      #2

      Moved to C++ gurus, as this isn't Qt-related at all.

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on 8 Mar 2012, 07:50 last edited by
        #3

        Hi,

        snprintf is not a function of Qt, and also not of the STL (namespace std).
        Which platform and which compiler tool chain are you using?

        I found this page, which describes that there are versions of printf in std namespace (include cstdio) but I've never seen that before.

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • B Offline
          B Offline
          broadpeak
          wrote on 8 Mar 2012, 08:35 last edited by
          #4

          #include <cstdio>
          std::snprintf();

          As I know the snprintf is conform only to C99 and C++11, so you have to use THAT compiler. GNU C++ 4.7 or later version is required to this.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lgeyer
            wrote on 8 Mar 2012, 12:17 last edited by
            #5

            There is qsnprintf(), a portable version of snprintf(); the "correct" way of doing such things however is using QString::sprintf().

            1 Reply Last reply
            1
            • B Offline
              B Offline
              broadpeak
              wrote on 8 Mar 2012, 15:52 last edited by
              #6

              [quote author="Lukas Geyer" date="1331209027"]There is qsnprintf(), a portable version of snprintf(); the "correct" way of doing such things however is using QString::sprintf().[/quote]

              Hm... According to this site:
              http://qt-project.org/doc/qt-4.8/qstring.html#sprintf
              "Warning: We do not recommend using QString::sprintf() in new Qt code."

              snprintf is a buffer_overrun_safe function, this is relatively new thing in the C/C++ world, so it can be used only under the newest C and C++ compilers.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mlong
                wrote on 8 Mar 2012, 17:39 last edited by
                #7

                Actually, I've found very little use to use any of the printf-related functions in Qt, as there are native QString methods for formatting and manipulating string contents appropriately. Depending on what you'd like to use snprintf() for, there is most likely a native Qt solution available through QString that would be more appropriate.

                Software Engineer
                My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Psycho_Path
                  wrote on 9 Mar 2012, 17:21 last edited by
                  #8

                  Thanks for the help. Can't say I've solved my problem, but to do that I'll be posting a new thread in the Qt Dev forum as the issue is related to the pixmap's loadFromData method.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    Morbius
                    wrote on 20 Feb 2019, 21:37 last edited by
                    #9

                    It would be:

                    #include <cstdio>

                    ...

                    char buf[4];
                    std::snprintf(buf, 4, "foo");
                    qDebug() << QString(buf);

                    or you can use qsnprintf();.

                    If you want Unicode, you have to use STL:

                    wchar_t buf[4];
                    std::swprintf(buf, 4, L"foo");
                    qDebug() << QString().fromWCharArray(buf);

                    C 1 Reply Last reply 20 Feb 2019, 22:00
                    0
                    • M Morbius
                      20 Feb 2019, 21:37

                      It would be:

                      #include <cstdio>

                      ...

                      char buf[4];
                      std::snprintf(buf, 4, "foo");
                      qDebug() << QString(buf);

                      or you can use qsnprintf();.

                      If you want Unicode, you have to use STL:

                      wchar_t buf[4];
                      std::swprintf(buf, 4, L"foo");
                      qDebug() << QString().fromWCharArray(buf);

                      C Offline
                      C Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 20 Feb 2019, 22:00 last edited by
                      #10

                      @Morbius said in Snprintf usage&#63;:

                      If you want Unicode, you have to use STL:
                      wchar_t buf[4];
                      std::swprintf(buf, 4, L"foo");
                      qDebug() << QString().fromWCharArray(buf);

                      No, you don't. You can simply do qDebug() << QStringLiteral(u"foo");, and please don't use wchar_t if you don't have to. It's not portable and annoying. There are modern replacements like char16_t.

                      1 Reply Last reply
                      4

                      • Login

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