Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. what is keyword i have to use instead snprintf in qt ?
Forum Updated to NodeBB v4.3 + New Features

what is keyword i have to use instead snprintf in qt ?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 740 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    I want to implement below function in qt . so i need guide regarding which function of qt i need to choose instead snprintf, printf and exit(1) ?

     static char filename[20];
      static int adapter_nr = 2;
    
      snprintf (filename, 19, "/dev/i2c-%d", adapter_nr);
      filehandle = open(filename,O_RDWR);
      if (filehandle < 0) {
        printf ("Error : Can't open /dev/i2c-%d\n",adapter_nr);
        /* ERROR HANDLING; you can check errno to see what went wrong */
        exit(1);
      }
    
      return 0;
    
    jsulmJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      I want to implement below function in qt . so i need guide regarding which function of qt i need to choose instead snprintf, printf and exit(1) ?

       static char filename[20];
        static int adapter_nr = 2;
      
        snprintf (filename, 19, "/dev/i2c-%d", adapter_nr);
        filehandle = open(filename,O_RDWR);
        if (filehandle < 0) {
          printf ("Error : Can't open /dev/i2c-%d\n",adapter_nr);
          /* ERROR HANDLING; you can check errno to see what went wrong */
          exit(1);
        }
      
        return 0;
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @Qt-embedded-developer

      • printf - std::cout
      • exit - https://doc.qt.io/qt-5/qcoreapplication.html#exit
      • snprintf - see all the QString::arg methods like https://doc.qt.io/qt-5/qstring.html#arg
      • File handling - QFile

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #2

        Hi

        instead snprintf, printf and exit(1) ?

        • snprintf = QStrings arg. https://doc.qt.io/qt-5/qstring.html#arg

        • printf = You can still use it if you want. but printing to console in a GUI app is mostly for logging.
          For user feed back, you can use https://doc.qt.io/qt-5/qmessagebox.html
          TO show info to the end user.

        • exit(1); will still work and terminate the app. You might not want to do that for a GUI program and rather
          display info to user.

        Q 1 Reply Last reply
        4
        • Q Qt embedded developer

          I want to implement below function in qt . so i need guide regarding which function of qt i need to choose instead snprintf, printf and exit(1) ?

           static char filename[20];
            static int adapter_nr = 2;
          
            snprintf (filename, 19, "/dev/i2c-%d", adapter_nr);
            filehandle = open(filename,O_RDWR);
            if (filehandle < 0) {
              printf ("Error : Can't open /dev/i2c-%d\n",adapter_nr);
              /* ERROR HANDLING; you can check errno to see what went wrong */
              exit(1);
            }
          
            return 0;
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #3

          @Qt-embedded-developer

          • printf - std::cout
          • exit - https://doc.qt.io/qt-5/qcoreapplication.html#exit
          • snprintf - see all the QString::arg methods like https://doc.qt.io/qt-5/qstring.html#arg
          • File handling - QFile

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • mrjjM mrjj

            Hi

            instead snprintf, printf and exit(1) ?

            • snprintf = QStrings arg. https://doc.qt.io/qt-5/qstring.html#arg

            • printf = You can still use it if you want. but printing to console in a GUI app is mostly for logging.
              For user feed back, you can use https://doc.qt.io/qt-5/qmessagebox.html
              TO show info to the end user.

            • exit(1); will still work and terminate the app. You might not want to do that for a GUI program and rather
              display info to user.

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #4

            @mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.

            i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?

            mrjjM jsulmJ 2 Replies Last reply
            0
            • Q Qt embedded developer

              @mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.

              i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #5

              Hi

              @mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.

              I think you dont want to use exit at all but simply return the error code, from errno - maybe so the calling function can know it went wrong.

              i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?
              Well your buffer is filename[20]; so you can overflow it.

              With QString there is no such buffer directly so most likely it won't be an issue as paths won't be that long anyway.

              1 Reply Last reply
              1
              • Q Qt embedded developer

                @mrjj Yes i might not want to use exit for an gui program. so i have to use return some value other than 0 to indicate this function failed.

                i heard that snprintf stop crash if more than 20 character i write into filename array . will same thing possible with QStrings arg ?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Qt-embedded-developer said in what is keyword i have to use instead snprintf in qt ?:

                i heard that snprintf stop crash if more than 20 character

                Do you mean it crashes with < 20 characters?! Where did you here that and which implementation was it? I never heard about QString::arg crashing, so just use it (you can also write tests for your use cases to make sure it isn't crashing).

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Q 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Qt-embedded-developer said in what is keyword i have to use instead snprintf in qt ?:

                  i heard that snprintf stop crash if more than 20 character

                  Do you mean it crashes with < 20 characters?! Where did you here that and which implementation was it? I never heard about QString::arg crashing, so just use it (you can also write tests for your use cases to make sure it isn't crashing).

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by
                  #7

                  @jsulm i am telling "more than 20 " means not <20 . its >20.

                  snprintf in c

                  jsulmJ 1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @jsulm i am telling "more than 20 " means not <20 . its >20.

                    snprintf in c

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @Qt-embedded-developer Well, you wrote: "i heard that snprintf stop crash if more than 20 character"...

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Q 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @Qt-embedded-developer Well, you wrote: "i heard that snprintf stop crash if more than 20 character"...

                      Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on last edited by Qt embedded developer
                      #9

                      @jsulm Sorry for not write clearly. when i write more than 20 characters using sprintf then it make crash program in c. so to avoid that situation. in c for safe side to avoid crash we use snprintf.

                      JonBJ 1 Reply Last reply
                      0
                      • Q Qt embedded developer

                        @jsulm Sorry for not write clearly. when i write more than 20 characters using sprintf then it make crash program in c. so to avoid that situation. in c for safe side to avoid crash we use snprintf.

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @Qt-embedded-developer
                        I think it most unlikely that an implementation of snprintf() will "crash" (or go wrong) if more than 20 characters, and I don't know where you think you have heard this. It would have been reported. So

                         static char filename[20];
                        snprintf (filename, 20, "/dev/i2c-%d", adapter_nr);
                        

                        should be fine.

                        However this doesn't matter if you use QString methods anyway.

                        1 Reply Last reply
                        0

                        • Login

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