Do you know why we use Command Prompt or Power Shell? Can we use Windows DOS Commands in a C++ app and in other operating systems? In this post, we will explain how you can run Windows DOS commands in C++.
Apple DOS was the first family of Disk Operating Systems (DOS) for the first Apple II series between 1978 to 1983. Apple DOS has three major releases: DOS 3.1, DOS 3.2, and DOS 3.3. IBM licensed and re-released PC DOS 1.0 in 1981 for use in its x86 PCs. MS-DOS (Microsoft Disk Operating System) was another DOS initially developed in parallel. These two products diverged after twelve years, in 1993, with recognizable differences in compatibility, syntax, and capabilities.
DOS (Disk Operating System) is a command and text-based operating system that primarily developed for x86 personal computers (now it supports both 32bit and 64bit operating systems). It is found that using graphics is makes a lot of things easier and better than writing commands in a console. That resulted with the development of Mac-OS, Amiga-OS, Windows, and other UI-based operating systems. After many years of graphical user interfaces, GUIs, we still have Command Prompt or Power Shell which runs these DOS commands. Mac-OS is changed its base to Linux, but it has also about the same DOS commands on Linux.
When we type commands into the command prompt on Windows, COMMAND.COM executable parse the input line and it matches a command name to a built-in command or to the name of an executable program file or batch file on disk. Some C and C++ compilers can be used by the DOS commands which means you don’t need a UI-based operating system to run your commands.
Today Windows is still evolving and has progressed in graphical sophistication. Yet we can still use DOS commands on Windows. Some of these commands are very useful to create a folder or to copy or move files. There might be other console-based commands, programming languages, and similar beneficial console applications. All of these kinds of commands can be run programmatically by C++ code. In this post, we will explain how to run DOS commands.
How to use system commands in a C++ app
In C++, we can run system commands – in Windows these are MS-DOS commands – by using the system() C++ command. It is very easy to use, just write your command as text in the system command. For example cls command which clears DOS screen, we can run this in C++ as below,
1 2 3 |
system("cls"); |
In some console applications, clearing screen may be useful to output time based or loop based variables. For example this simple example clears the screen and writes count.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <Windows.h> // Needed to use Sleep() command int main() { for(int i =0; i<100; i++) { system("cls"); std::cout << i << 'n'; Sleep(10); } getchar(); return 0; } |
This example creates a new directory by using mkdir command and it copies x folder to y folder by the xcopy command.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> int main() { system("mkdir D:y"); system("xcopy D:x D:y /s /q"); getchar(); return 0; } |
As you see we can easily reach and use DOS commands in the latest C++ versions. C++ Builder and other CLANG compilers supports this . Note that sometimes using dos command may help you much to use 3rd party commands. For example you can use console based zip , rar, unrar commands to pack or unpack your application files, data files etc.. You can also run your other console applications that you develop in C++.
Try these examples for yourself – download a free trial of C++ Builder.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition