Detailed Explanation of Common Parameters of C/C++ system() Function


foreword

The system() function is suitable for C/C++ programs to call operating system commands, and is often used in our C++ programming. Here is a detailed record of the common parameters of the system() function in C++ programming.


1. Common recommendations

  • pause: Pauses the processing of the batch file and displays a message

  • color: Set the default console foreground and background colors

  • title: Set the window title of the cmd.exe session

  • cls: clear the screen

system("pause");//暂停
system("color 0B");//0B为控制台的前景和背景颜色,详细介绍请见下表
system("title winName");//winName为控制台名称
system("cls");//清屏

The parameter after the color parameter is a single digit in hexadecimal, and the corresponding digit and color are shown in the table below

0 1 2 3 4 5 6 7 8 9 A B C D E F
black blue green Lake Blue red Purple yellow White grey light blue light green light green light red lavender pale yellow bright white

2. File operation

  • md/mkdir: create a directory

  • start: Start a separate window to run the specified program or command

  • del: delete at least one file

  • copy:       copy at least one file to another location

  • move: move one or more files from one directory to another

  • type: display the content of the text file

system("md name");//等同于system("mkdir name");新建名为name的文件夹
system("start cmd");//打开命令提示符
system("start https://www.baidu.com/")//打开网页
system("del file.txt");//删除file.txt文件
system("copy A B");//目录文件A复制到目录文件B 可以添加路径,文件需要大量拷贝的情况下很常用
system("move A B");//目录文件A移动到目录路径B 当需要转移一个文件到另一个目录是很常用
system("type file.txt");//显示file.txt文件中内容

3. System operation

  • shutdown: Allows the computer to be properly shut down, either locally or remotely

  • date: display or set the date

  • time: display or set the system time

  • cd/chdir: Display the name of the current directory or change it

  • dir: Display the files and subdirectories in a directory

  • ver: display the version of Windows

  • vol: Display disk volume label and serial number

system("shutdown -s –t 60");//60s后关机
system("shutdown -a");//取消关机
system("shutdown -p");//立即关闭
system("shutdown -1");//注销计算机
system("shutdown -r –t 0");//立即重启
system("shutdown -h –f");//睡眠
system("date");//年月日
system("time");//时分秒
system("cd");//等同于system("chdir");显示文件路径
system("dir");//显示当前路径下所有文件
system("ver");//显示Windows系统版本
system("vol");//显示磁盘卷标和序列号

Summarize

This article records the function of the parameters of the system() function and how to use it. The system() function provides a large number of methods that allow us to process data quickly and conveniently.

Guess you like

Origin blog.csdn.net/Gary_ghw/article/details/131206596