Site icon Learn 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.

[crayon-66346adadc509599973873/]

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”),

[crayon-66346adadc514013640412/]

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

[crayon-66346adadc518642909294/]

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

[crayon-66346adadc51a142581364/]

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,

[crayon-66346adadc51d656322695/]

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.

[crayon-66346adadc51f518818326/]

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.

Exit mobile version