[Solved] : Problem with QString in system call

  • 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?


  • Try in this way:

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

  • Lifetime Qt Champion


    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.

  • 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.

  • 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.

  • 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));

  • That's it.
    If I had

    just before calling the command, it works.

  • 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?

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

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

  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.