使用C语言编写一个强制关机程序

  #include

  #include

  #include

  #include

  #include

  #include

  int main()

  {

  char input[10];

  printf("警告!您的计算机将在一分钟后关机,请保存好您的工作并退出所有程序!");

  printf("是否确认关机?(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 - 用户选择了关机

  ",

  localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,

  localTime->tm_hour, localTime->tm_min, localTime->tm_sec);

  fclose(logFile);

  }

  // 执行滑动关机操作

  system("shutdown /r /t 60");

  // 显示倒计时

  for (int i = 60; i >= 0; i--) {

  printf("

  倒计时:%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 - 用户选择了关机并取消关机

  ",

  localTime->tm_year + 1900, localTime->tm_mon + 1, localTime->tm_mday,

  localTime->tm_hour, localTime->tm_min, localTime->tm_sec);

  fclose(logFile);

  }

  // 执行取消关机操作

  system("shutdown /a");

  return 0;

  }

  }

  }

  return 0;

  } else if (strcmp(input, "n") == 0 || strcmp(input, "N") == 0) {

  printf("取消关机操作。");

  return 0;

  } else {

  printf("输入错误,请重新输入");

  printf("是否确认关机?(y/n): ");

  scanf("%s", input);

  }

  }

  return 0;

  }