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.
Table of Contents
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.
1 2 3 |
std::system("dir"); |
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”),
1 2 3 |
std::system("dir >> tmp.out"); |
After running this execution, you can read this output file in C++.
1 2 3 |
Memo1->Lines->LoadFromFile("tmp.out"); |
and you can remove this output after you have read it.
1 2 3 |
std::remove("tmp.out"); |
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,
1 2 3 4 5 |
std::system("dir > tmp.out"); Memo1->Lines->LoadFromFile("tmp.out"); std::remove("tmp.out"); |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#include <vcl.h> #pragma hdrstop #include "Executor_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void executor(UnicodeString input, TMemo *memo) { std::system( AnsiString(input+" > tmp.out").c_str() ); Memo1->Lines->LoadFromFile("tmp.out"); std::remove("tmp.out"); } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { switch(Key) { case 13: executor(Edit1->Text, Memo1); break; } } |
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/
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.