使用C語言編寫一個強制關(guān)機程序
一、項目介紹
C語言實現(xiàn)一個簡單的"流氓軟件",一個可以強制關(guān)機惡作劇關(guān)機程序,輸入指定指令可以解除
二、運行截圖

然后當(dāng)你輸入“n”才可以解鎖關(guān)機。

三、完整源碼
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <conio.h>
int main()
{
char input[10];
printf("警告!您的計算機將在一分鐘后關(guān)機,請保存好您的工作并退出所有程序!");
printf("是否確認(rèn)關(guān)機?(y/n): ");
scanf("%s", input);
while (1) {
if (strcmp(input, "y") == 0 || strcmp(input, "Y") == 0) {
// 記錄用戶選擇到日志文件
FILE *logFile = fopen("D:/a1.txt", "a");
if (logFile != NULL) {
time_t currentTime;
struct tm *localTime;
time(¤tTime);
localTime = localtime(¤tTime);
fprintf(logFile, "%04d-%02d-%02d %02d:%02d:%02d - 用戶選擇了關(guān)機\n",
localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,
localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
fclose(logFile);
}
// 執(zhí)行滑動關(guān)機操作
system("shutdown /r /t 60");
// 顯示倒計時
for (int i = 60; i >= 0; i--) {
printf("\r倒計時:%d秒", i);
fflush(stdout);
sleep(1);
if (_kbhit()) { // 檢測按鍵
char key = _getch(); // 獲取按鍵值
if (key == 's' || key == 'S') { // 如果按下了's'或'S'鍵
// 記錄用戶選擇到日志文件
FILE *logFile = fopen("D:/a1.txt", "a");
if (logFile != NULL) {
time_t currentTime;
struct tm *localTime;
time(¤tTime);
localTime = localtime(¤tTime);
fprintf(logFile, "%04d-%02d-%02d %02d:%02d:%02d - 用戶選擇了關(guān)機并取消關(guān)機\n",
localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,
localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
fclose(logFile);
}
// 執(zhí)行取消關(guān)機操作
system("shutdown /a");
return 0;
}
}
}
return 0;
} else if (strcmp(input, "n") == 0 || strcmp(input, "N") == 0) {
printf("取消關(guān)機操作。");
return 0;
} else {
printf("輸入錯誤,請重新輸入");
printf("是否確認(rèn)關(guān)機?(y/n): ");
scanf("%s", input);
}
}
return 0;
}程序首先顯示警告信息,并詢問用戶是否確認(rèn)關(guān)機。如果用戶輸入"y"或"Y",則程序會記錄用戶的選擇到日志文件(D:/a1.txt),然后執(zhí)行關(guān)機操作,并在倒計時60秒后關(guān)閉計算機。如果用戶輸入"n"或"N",則程序會取消關(guān)機操作。如果用戶輸入其他字符,則程序會提示輸入錯誤并重新詢問用戶是否確認(rèn)關(guān)機。
到此這篇關(guān)于使用C語言編寫一個強制關(guān)機程序的文章就介紹到這了,更多相關(guān)C語言關(guān)機內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C/C++中的mem函數(shù)和strcopy函數(shù)的區(qū)別和應(yīng)用
strcpy和memcpy都是標(biāo)準(zhǔn)C庫函數(shù),strcpy提供了字符串的復(fù)制而memcpy提供了一般內(nèi)存的復(fù)制。下面通過本文重點給大家介紹C/C++中的mem函數(shù)和strcopy函數(shù)的區(qū)別和應(yīng)用,非常不錯,感興趣的朋友一起看下吧2016-08-08
Qt6.3 + Clion +MSVC2019環(huán)境配置詳解
本文主要介紹了Qt6.3 + Clion +MSVC2019環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
c語言之char*和unsigned?char*的區(qū)別及說明
這篇文章主要介紹了c語言之char*和unsigned?char*的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
C++標(biāo)準(zhǔn)模板庫string類的介紹與使用講解
今天小編就為大家分享一篇關(guān)于C++標(biāo)準(zhǔn)模板庫string類的介紹與使用講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

