[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?
Thanks
-
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.
-
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));
fill_file_function(os);@