C语言编写简单的定时关机程序

Zarah ·
更新时间:2024-09-20
· 960 次阅读

写一个定时关机的小程序,可以立即关闭计算机,也可以一段时间后关闭计算机。

这里主要考察system()命令。

代码实现:

#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char cmd[20]="shutdown -s -t "; char t[5]="0"; int c; system("title C语言关机程序"); //设置cmd窗口标题 system("mode con cols=48 lines=25"); //窗口宽度高度 system("color f0"); //可以写成 red 调出颜色组 system("date /T"); system("TIME /T"); printf("----------- C语言关机程序 -----------\n"); printf("1.实现10分钟内的定时关闭计算机\n"); printf("2.立即关闭计算机\n"); printf("3.注销计算机\n"); printf("0.退出系统\n"); printf("-------------------------------------\n"); scanf("%d",&c); switch(c) { case 1: printf("您想在多少秒后自动关闭计算机?(0~600)\n"); scanf("%s",t); system(strcat(cmd,t)); break; case 2: system("shutdown -p"); break; case 3: system("shutdown -l"); break; case 0: break; default: printf("Error!\n"); } system("pause"); return 0; }

这个程序虽然实用价值不大,但是可以让我们了解 system() 函数。

在Windows下,system() 函数可以执行 dos 命令;在 Unix/Linux 中,可以执行Shell。

请在Windows下运行上面的程序。程序中对dos界面的设置和关机功能都是通过dos命令实现的。

您可能感兴趣的文章:vc6.0中c语言控制台程序中的定时技术(定时器)c语言定时器示例分享



c语言编写 定时关机 程序 C语言

需要 登录 后方可回复, 如果你还没有账号请 注册新账号