- gf24118 的博客
DC・system & sleep
- 2025-9-24 12:44:40 @
DC series catalog direct access
Preface (must read)
Click to close/expand (recommended to view and click)
This blog marks the beginning of the DC series. Today's blog will talk about [ the usage of system and sleep ].
Also, here's a recommended blog, which serves as an example of how the blog content is utilized.
system
To use the system function in Dev-C++, it is necessary to include the header file #include <process.h>. This function is used to execute Windows commands or external programs. The following is the specific usage:
Function Definition
The system function is used to execute the passed string as a system command, and its prototype is:
int system(const char *command);
For example, system ("dir") can execute the dir command from the Windows command prompt.
Common application examples
-
Clear screen: System (CLS) clears console output.
-
Freeze screen: System ("pause") pauses program execution for easier observation of output results.
-
Change console color: system ("color 0A") (where 0 is the background color code and A is the text color code).
-
Execute external programs such as system ("notepad. exe") to open Notepad.
Advanced features
1.System Management:
-
Shutdown/restart: system ("shutdown - s - t 0") (Windows shuts down immediately) or system ("power-off") (Linux shuts down).
-
Terminate process: system ("taskkill/F/IM notepad. exe") (Windows) or system ("kill -9 1234") (Linux).
2.File operation:
-
Create directory: system ("mkdir folder") (cross platform conditional compilation processing path differences).
-
File operation: system ("copy file1. txt file2. txt") (Windows) or system ("cp file1. txt file2. txt") (Linux).
Sleep
In C++, sleep related functions are mainly used for delaying operations of threads or processes.
Basic usage: It needs to include the<windows. h>header file, the function name is Sleep (capitalized), and the parameter unit is milliseconds. For example, Sleep (1000) represents a 1-second pause.
Example:
#include <windows.h>
int main() {
Sleep(500); //Pause for 500 milliseconds
return 0;
}
Chinese
DC系列目录直接访问
前言(必须阅读)
system
要使用Dev-C++中的系统功能,必须包含头文件 #include <process.h>. 此函数用于执行Windows命令或外部程序。具体用法如下:
函数定义
system函数用于将传递的字符串作为系统命令执行,其原型为:
int system(const char *command);
例如,system(“dir”)可以从Windows命令提示符执行dir命令。
常见应用示例
-
清除屏幕:System (CLS)清除控制台输出。
-
冻结屏幕:System ("pause")暂停程序执行,以便更容易观察输出结果。
-
更改控制台颜色:system ("color 0A")[其中0是背景颜色代码,A是文本颜色代码]。
-
执行system ("notepad. exe") 等外部程序打开记事本。
高级功能
1.系统管理:
-
关机/重启:system ("shutdown - s - t 0")[Windows立即关闭]或system ("power-off")[Linux关闭]。
-
终止进程:system ("taskkill/F/IM notepad. exe") [Windows]或system ("kill -9 1234") [Linux]。
2.文件操作:
-
创建目录:system(“mkdir文件夹”)(跨平台条件编译处理路径差异)。
-
文件操作:system ("copy file1. txt file2. txt")[Windows]或system ("cp file1. txt file2. txt") [Linux]。
Sleep
在C++中,睡眠相关函数主要用于延迟线程或进程的操作。
基本用法:它需要包括<windows。h>头文件,函数名为Sleep(大写),参数单位为毫秒。例如,Sleep(1000)表示1秒的暂停。
例子:
#include <windows.h>
int main() {
Sleep(500); //Pause for 500 milliseconds
return 0;
}