Invoking the DOS prompt using the C system() function
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Invoking the DOS prompt using the C system() function
To show: How to use the C system() function to invoke the DOS prompt and commands
// Demonstrates the system() C function, invoking the DOS prompt and commands
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
// Declare a buffer to hold inputs
char input[40];
// while true
while (1)
{
// read the user input
puts("\nInput the desired DOS command, blank to exit");
gets_s(input, 40);
// Exit if a blank line was entered
if(input[0] == '\0')
exit(0);
// Execute the command
system(input);
}
}
Output example:
Input the desired DOS command, blank to exit
dir
Volume in drive F is hd0c
Volume Serial Number is 98DB-52B1
Directory of f:\vc2005project\myaddr\myaddr
11/13/2006 01:28 PM <DIR> .
11/13/2006 01:28 PM <DIR> ..
11/12/2006 11:32 AM 799 arithmet.h
11/13/2006 01:28 PM <DIR> Debug
11/13/2006 01:28 PM 427 myaddr.cpp
11/13/2006 12:59 PM 4,011 myaddr.vcproj
11/13/2006 01:30 PM 1,423 myaddr.vcproj.MYPERSONAL.Johnny.user
4 File(s) 6,660 bytes
3 Dir(s) 8,661,561,344 bytes free
Input the desired DOS command, blank to exit
mem /C
Conventional Memory :
Name Size in Decimal Size in Hex
------------- --------------------- -------------
MSDOS 12000 ( 11.7K) 2EE0
KBD 3296 ( 3.2K) CE0
HIMEM 1248 ( 1.2K) 4E0
COMMAND 5472 ( 5.3K) 1560
DOSX 34720 ( 33.9K) 87A0
FREE 112 ( 0.1K) 70
FREE 598320 (584.3K) 92130
Total FREE : 598432 (584.4K)
Upper Memory :
Name Size in Decimal Size in Hex
------------- --------------------- -------------
SYSTEM 229360 (224.0K) 37FF0
DOSX 128 ( 0.1K) 80
MOUSE 12528 ( 12.2K) 30F0
MSCDEXNT 464 ( 0.5K) 1D0
REDIR 2672 ( 2.6K) A70
FREE 2656 ( 2.6K) A60
FREE 14208 ( 13.9K) 3780
Total FREE : 16864 ( 16.5K)
Total bytes available to programs (Conventional+Upper) : 615296 (600.9K)
Largest executable program size : 595616 (581.7K)
Largest available upper memory block : 14208 ( 13.9K)
1048576 bytes total contiguous extended memory
0 bytes available contiguous extended memory
941056 bytes available XMS memory
MS-DOS resident in High Memory Area
Input the desired DOS command, blank to exit
Press any key to continue . . .