Explanation of system function usage in C language

Source: Internet
Author: User

The system function can invoke some DOS commands, such as System ("CLS");//clear screen, equal to the use of CLS command on DOS

================================================================
The following list of commonly used DOS commands can be called with the system function:
ASSOC Displays or modifies file name extension associations.
At Schedules commands and programs to run on the computer.
ATTRIB Display or change file properties.
Break sets or clears the extended CTRL + C check.
CACLS displays or modifies the file's access control List (ACLs).
Call calls this one from another batch program.
The CD displays the name of the current directory or changes it.
CHCP Displays or sets the number of active code pages.
CHDIR displays the name of the current directory or changes it.
CHKDSK checks the disk and displays the status report.
CHKNTFS Displays or modifies the startup time disk check.
CLS clears the screen.
CMD opens another Windows command interpreter window.
Color sets the default console foreground and background color.
COMP compares the contents of two or two sets of files.
Compact displays or changes the compression of files on NTFS partitions.
Convert converts a FAT volume to NTFS. You cannot convert
The current drive.
Copy copies at least one of the files to another location.
Date Displays or sets dates.
DEL deletes at least one file.
DIR displays the files and subdirectories in a directory.
DISKCOMP compares the contents of two floppy disks.
DISKCOPY Copy the contents of one floppy disk to another.
DOSKEY Edit the command line, invoke the Windows command, and create a macro.
echo Displays the message, or turns the command back on or off.
Endlocal to end localization of environment changes in batch files.
ERASE Delete at least one file.
Exit CMD. EXE Program (command interpreter).
FC compares two or two sets of files and displays
Different places.
Find searches the file for a text string.
FINDSTR searches for strings in the file.
For each file in a set of files, run a specified command
Format formats the disk for use with Windows.
FTYPE displays or modifies the file types used for file name extension associations.
GOTO to point the Windows command interpreter to a batch program
One of the indicated rows in the
GRAFTABL enable Windows to display in image mode
Extended character Set.
Help provides information about Windows commands.
IF you perform conditional processing in a batch program.
Label creates, changes, or deletes a disk's volume label.
MD to create a directory.
MKDIR Create a directory.
MODE configures the system device.
More displays a result screen at a time.
Move moves files from one directory to another.
Path displays or sets the search path for the executable file.
Pause pauses processing of batch files and displays messages.
POPD restores the previous value of the current directory saved by PUSHD.
Print prints a text file.
PROMPT change the Windows command prompt.
PUSHD Save the current directory and make changes to it.
RD Delete directory.
RECOVER recovers readable information from the problematic disk.
REM record batch file or CONFIG. The comment in SYS.
REN renames the file.
RENAME Rename the file.
Replace replaces the file.
RMDIR Delete the directory.
Set to display, set, or remove Windows environment variables.
SETLOCAL the localization of the environment changes in the batch file.
SHIFT replaces the position of replaceable parameters in the batch file.
Sort to categorize the input.
Start starts another window to run the specified program or command.
SUBST associates the path with a drive letter.
Time displays or sets the system times.
The TITLE sets CMD. The window caption of the EXE session.
Tree displays the directory structure of a drive or path in graphical mode.
TYPE Displays the contents of the text file.
VER shows the version of Windows.
VERIFY tell Windows whether to verify that the file is correct
Write to disk.
VOL Displays the disk volume label and serial number.
XCOPY copies files and directory trees.
=====================================================================
Function: Issue a DOS command
Usage: int system (char *command);
The system function has been included in the standard C library and can be called directly
program Example:
#include <stdlib.h>
#include <stdio.h>


int main (void)
{
printf ("About to spawn Command.com and run a DOS command\n");
System ("dir");
return 0;
}

also such as: System ("pause") can be implemented to freeze the screen, easy to observe the execution results of the program, the System ("CLS") can achieve clear screen operation. The call to the color function can change the foreground color and background of the console, as specified below.
For example, with System ("Color 0A"), where color after 0 is the background color designator, A is the foreground color code. Each color code is as follows:
0 = black 1= Blue 2 = Green 3= Lake Blue 4 = Red 5 = Purple 6 = Yellow 7 = White 8 = Gray 9 = light blue a= light green b= light green c= light Red d= lavender e= light yellow f= bright white
(note: Microsoft Visual C + + 6.0 supports system)
look at the following example, I believe you will learn more about the application of system in C programming.

Example:
C calls the DOS command to implement a timed shutdown:
#include <stdio.h>
#include <string.h>
#include <  stdlib.h>
int print ()
{
printf ("╪╪╪╪╪╪╧╧╧╧╧╧╧╧╪╪╪╪╪╪\n");
printf ("╔═══╧╧c language Shutdown Program ╧╧═══╗\n");
printf ("║※1.10 minutes of scheduled shutdown computer ║\n");
printf ("║※2. Turn off computer ║\n immediately");
printf ("║※3. Logoff computer ║\n");
printf ("║※0. Exit system ║\n");
printf ("╚═══════════════════╝\n");
return 0;
}


void Main ()
{
System ("Title C language Shutdown Program");//Set CMD window caption
System ("mode con cols=48 lines=25");//Window width height
System ("Color 0B");
System ("date/t");
System ("time/t");
Char cmd[20]= "shutdown-s-T";
Char t[5]= "0";
Print ();
int C;
scanf ("%d", &c);
GetChar ();
Switch (c)
{
Case 1:printf ("How many seconds do you want to turn off the computer automatically?") (0~600) \ n "); scanf ("%s ", t); system (strcat (cmd,t)); break;
Case 2:system ("shutdown-p");
Case 3:system ("shutdown-l");
Case 0:break;
default:printf ("error!\n");
}
System ("pause");
Exit (0);
}

Example two:
Delete the file in C, for example, the location of the file is D:\123.txt
Execute Windows commands with the system () function.
#include <stdlib.h>
#include <stdio.h>

int main (void)
{
System ("Del d:\123.txt");
return 0;
}

Explanation of system function usage in C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.