0

I'm trying to run an external executable (code below) in Qt as a separate process.

test.c:

#include <stdio.h>
int main () {
    FILE *f;
    f = fopen("a.txt", "w");
    fprintf(f, "1\n");
    fclose(f);
    return 1;
}

and in Qt I have:

QProcess* process = new QProcess();
QString program = "/Users/myUser/Desktop/a.out";
process->execute(program);

I've read up on the differences between execute(), start(), and startDetached() and to my understanding I want to use execute() because I want the process running the external executable to finish before continuing execution in the main process. However I've tried all three expecting to find a file a.txt containing the text "1" in it, but it doesn't exist. Any help or suggestions as to why it's not working? Thanks!

4
  • 1
    QProcess::execute is a static function so you don't have to create a new instance of QProcess. Just to clarify - do you want the calling program to wait for the process to finish before continuing execution? Are you sure the path to the executable is correct? What directory are you running the parent program from (the a.txt may be in that directory if it isn't the same as a.out's directory). Commented Jul 2, 2015 at 1:41
  • If the process runs successfully, it'll create he output in QDir::current(). I'm pretty sure you haven't looked there for the output file. Commented Jul 2, 2015 at 16:11
  • @Sam, Yeah, I want the calling program to remain executing only after the child process has completely finished. I just use the file system search to look for a.txt because it's not in any of the places I would expect it to be. I appreciate the suggestions though! Any more help is greatly appreciated. Commented Jul 2, 2015 at 16:16
  • In your test program, try writing to a specific path (e.g. /tmp/a.txt). Commented Jul 6, 2015 at 0:56

1 Answer 1

2

Check in the main() -function that a.txt -file really exists and is open before writing to it.

Check in the Qt that the "program" -file really exists before executing it.

Return different result codes from the main() -function and check the result in Qt:

QProcess *proc = new QProcess();

proc->start(program);
proc->waitForFinished();

QString result=proc->readAllStandardOutput();

// Check result here
Sign up to request clarification or add additional context in comments.

4 Comments

When I run the executable from the command line independent of Qt, it works fine and creates a.txt if that file doesn't already exist. It's just when I try running it from Qt nothing happens. Can you explain why you use proc->start(program); proc->waitForFinished(); instead of just process->execute(program); ? Thanks
The example code has been copied from my working code.
I think QProcess::execute is used just for convenience so you don't have to create a new QProcess object if all you want to do is run the process and wait for it to finish. If you want to read the output of the process or do anything more complex then you can do it the way @PetriPyöriä has done it.
So it turns out when I tried your solution I removed the absolute path from my program QString... that was the issue. Your answer works. Thanks to both of you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.