C 程序:关闭 Windows/Linux 机器
原文:https://www.studytonight.com/c/programs/misc/windows-shutdown-program
这个程序关闭你的电脑系统。stdlib.h
的系统功能是运行一个可执行文件 shutdown.exe,它存在于 Windows 7 和 XP 的C:\WINDOWS\system32
文件夹中。
下面是一个关闭 Windows 7 的程序。
#include<stdio.h>
#include<stdlib.h> // to use system() method
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown your pc now (y/n)?");
scanf("%c", &ch);
if(ch == 'y'|| ch == 'Y')
{ /*
/s is used to order the compiler
to shutdown the PC
*/
system("C:\\WINDOWS\\System32\\shutdown /s");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
执行shutdown.exe时可以使用各种选项,例如可以使用/t
选项指定关机发生的秒数。
- 语法:
"shutdown /s /t x";
这里 x 是关机的秒数。 - 示例:默认 30 秒后关机。要立即关机,可以写
"shutdown /s /t 0"
如果你希望重启你的电脑,那么你可以使用"shutdown /r"
。
关闭视窗操作系统的程序
下面是一个关闭 Windows XP 的程序。
#include<stdio.h>
#include<stdlib.h> // to use system() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown the PC- (y/n) ?\n");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y')
{
system("C:\\WINDOWS\\System32\\shutdown -s");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
- 要立即关机,请使用
"C:\\WINDOWS\\System32\\shutdown -s -t 0"
。要重启,请使用"-r"
代替"-s"
。 - 为了更好的理解,请浏览关闭 Windows 7 的程序,其中详细解释了使用 t 和
r
代替s
。
*注: A '-' 在 Windows XP 中执行的功能与 Windows 7 情况下 '/' 执行的功能相同。*
关闭操作系统的程序
下面是一个关闭 Linux 操作系统的程序。
#include<stdio.h>
#include<stdlib.h> // to use system() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown your pc now(y/n)?");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y')
system("shutdown -P now");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
您需要以用户身份登录才能执行上述程序,否则您将收到关机消息:“需要 root”。
'-P'
选项指定您想要关闭机器的电源。 您可以将分钟指定为:shutdown -P "number of minutes"
* 如需更多帮助或选项,请在终端输入:man shutdown