thecaptain0220
thecaptain0220

Reputation: 2168

Receiving console output

Is there a way to execute a program and receive the console output in c++ instead of displaying the console window? I am trying to do a command line call but provide a GUI instead of the console output.

Upvotes: 1

Views: 166

Answers (2)

Stephen
Stephen

Reputation: 49224

You can write stdout to a file instead, and display the file in your GUI. One method for doing that is freopen.

int main ()
{
  freopen ("myfile.txt","w",stdout);
  printf ("This sentence is redirected to a file.");
  fclose (stdout);
  return 0;
}

This redirects stdout to myfile.txt.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490623

You can do this on most systems using popen (or on some compilers _popen). If that isn't versatile enough for your purposes, you'll probably have to do something platform specific (e.g., fork on a POSIX-like system, or CreateProcess on Windows).

Upvotes: 1

Related Questions