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. [Solved] : Problem with QString in system call

[Solved] : Problem with QString in system call

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 4.3k 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.
  • L Offline
    L Offline
    laetis
    wrote on last edited by
    #1

    Hi everyone,

    Let's say I want to replace a line from a file with a string .

    I first wrote something like:

    QString command="sed -i '2s/.*/string/' file";

           int system_exit=system(qPrintable(command));
    

    If I do that the line is changed but the rest of the file is erased.

    If I write:
    int system_exit=system("sed -i '2s/.*/string/' file");
    it works just fine.

    What's going on?

    Thanks

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nologinma
      wrote on last edited by
      #2

      Try in this way:

      @QString command=“sed -i ’2s/./string/’ file”;
      const char
      command2;
      command2 =command.toLocal8Bit().data();
      int system_exit=system(command2);
      @

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        @
        const char* command2;
        command2 =command.toLocal8Bit().data();
        @

        This is a bad idea, the QByteArray returned by toLocal8Bit ceases to exist at the end of this line. You should rather use an intermediate QByteArray variable and use the data from it.

        However, using QProcess might be a better idea.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nologinma
          wrote on last edited by
          #4

          I have run the following code without error:

          @ QString command= "dir .";
          const char* command2;
          command2 =command.toLocal8Bit().data();
          int system_exit;
          system_exit = system(command2);@

          and in my Console I saw the result.

          for me it is ok.

          bq. The wing structure of the Bumblebee, in relation to its weight, it is not airworthy, but he doesn't know and flies the same

          ;-)

          just to note

          I have try also the following code:

          @ QString command= "dir . ";
          int system_exit;
          system_exit = system(qPrintable(command));@

          and it work well.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            laetis
            wrote on last edited by
            #5

            With this

            @QString command=“sed -i ’2s/.*/string/’ file”;
            const char* command2;
            command2 =command.toLocal8Bit().data();
            int system_exit=system(command2);@
            

            I have the same issue, it is still erasing the end of the file. I precise that I have a problem only with sed.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              laetis
              wrote on last edited by
              #6

              The strangest thing is going on. I am trying that on the specific file I want to change and that is created and filled just before. If I try my sed a another file (preexisting) it works just fine.

              If I end the function after the creation of the file and create a new function juste for the sed that is called after the one creating the file (inside it is still not working). It works!

              Any ideas?

              I don't know how my file is filled, I am calling a library for that, And I am creating it this way:
              @QString file;
              ofstream os(qPrintable(file));
              fill_file_function(os);@

              1 Reply Last reply
              0
              • L Offline
                L Offline
                laetis
                wrote on last edited by
                #7

                That's it.
                If I had

                @os.close()@
                just before calling the command, it works.

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nologinma
                  wrote on last edited by
                  #8

                  I suspect the following.

                  Maybe the file you're working on has not yet been released and is not editable by the "system"

                  Can you share the code sequence are you using?

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    laetis
                    wrote on last edited by
                    #9

                    It is really no more than that:
                    @ QString file;
                    ofstream os(qPrintable(file));
                    fill_file_function(os);
                    @
                    I don't know what the function is doing but I guess it is not closing the file and that that was the problem

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nologinma
                      wrote on last edited by
                      #10

                      bq. to better understand the problem it would take more shared code

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        laetis
                        wrote on last edited by
                        #11

                        I understand and agree but I don't know what's is inside fill_file_function(), it's a library I installed (openbabel for chemistry). I would have like to understand but at least it is working now.

                        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