C++C++17Code SnippetLanguage FeatureLearn C++

How To Execute A Command And Get The Output In C++ Builder

How To Execute A Command And Get The Output In C++ Builder

Operating Systems consist of a lot of system commands, and In C++, we can run system commands by using the std::system() command. In C++ Builder, we can retrieve the output of these commands into components, for example into a Memo (TMemo), or into a ListView (TList). This allows you to use the benefits of operating system commands in activities such as Windows development. You can use any other back-end applications, including other installed programming languages like Python, Node.js, and so on. In this post, we explain how to execute a command and get the output in C++ Builder.

How do I execute a command in C++ on Windows?

In C++, we can run system commands – in Windows these are DOS commands which run in the Terminal or CMD Prompt window – by using the system() C++ command. It is very easy to use, just write your command as text in the system command. For example, we can use dir command that lists the current directory. We can run the dir command in C++ as shown below.

Using this example, you can run many DOS commands and other executable back-end applications. You can find more examples below.

How to execute a command and get the output in C++ Builder?

Operating Systems are evolving and have progressed in graphical sophistication. Some of the operating system commands are very useful to get system information, create a folder, or copy or move files. In C++ Builder, we can retrieve the output of these commands into components like a Memo (TMemo) or into a ListView (TList).

If you are able to retrieve the output of other executables, this allows you to use the benefits of not just the operating system commands; you can use any other backend applications, including other installed programming languages like Python, Node.js and others. Python has some very useful Artificial Intelligence libraries that are hard to find or emulate in other programming languages. Python also has powerful frameworks that allow you to analyze images, and videos with Deep Learning features. Of course, you can do the same in C++ too, but if you want to use the Python interpreter just run Python applications then you can get the output, or you can read output files. Very simple, and powerful.

If you want to execute a command or any executable in Windows, you can use the std::system method and you can point to a file by using “>” and the name of the file. Here is a very simple example that outputs to a file (i.e. “tmp.out”),

After running this execution, you can read this output file in C++.

and you can remove this output after you have read it.

Is there a simple example to execute a command and get the output in C++ Builder?

Here is a simple example that runs the Windows terminal dir command, print output to a file, and loads from this flame to a Memo component,

Is there a full example of how to execute a command and get the output in C++ Builder?

Here is a full C++ Builder VCL example.

  • Just create a new C++ Builder VCL Application,
  • Drag Edit and Memo components on to Form
  • Double click to OnKeyDown() event of Edit1
  • and modify C++ codes as below,

This full example gives output of any commands you type and enter as shown below.

As you see C++ Builder is really, you can read any text file into your Memo component by using LoadFromFile() method. Now you can easily use many DOS commands and other programming languages in your C++.

What other ways are there to get a C++ program to read and write to the Windows console?

There are other ways of interacting with the Windows terminal / console (another name for the DOS like window). These use slightly more advanced techniques which require you to read and write to the std:in and std:out virtual file handles or to directly call Windows API calls to do it. There is also a third-party library for some versions of C++ Builder available on GetIt which allows you to read and write to the Windows console. You can find the component here: https://getitnow.embarcadero.com/DOSCommand-1.5cb-Sydney/

How To Execute A Command And Get The Output In C++ Builder The C++ Builder Logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?