【问题标题】:How to pause the C program?如何暂停C程序?
【发布时间】:2017-04-18 14:51:06
【问题描述】:

已修复

system("read -r -p \"Press any key to continue...\" key")

我现在正在编写一个命令行工具并使用C语言,但是我想知道如何暂停程序? (不能中止它,但可以重新继续)我试图让它像“按 Enter 继续”。

我在Linux上所以我没有或者什么,我有,我尝试了“pause()”,但是这个函数不能实现“按Enter继续”,它不能重新继续。

想知道是否有一个功能或什么可以帮助我做到这一点?如果是,请告诉我,谢谢!

PS:这些是我上面的代码,如你所见,我正在使用一个名为“conti”的函数来设置暂停

另外:忽略那些“延迟”功能,只做测试。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void delay500ms(void){
    unsigned char i,j,k;
    for(i=15;i>0;i--)
        for(j=202;j>0;j--)
            for(k=81;k>0;k--);
}

void delay1s(void){
    unsigned char h,i,j,k;
    for(h=5;h>0;h--)
        for(i=4;i>0;i--)
            for(j=116;j>0;j--)
                for(k=214;k>0;k--);
}

int pause(void);

int ldown (int ln)
{
    int k;
    for (k=0;k<ln;k++)
    {
        printf("\n");
    }
}

void conti ()
{
    printf("\n\nPress Enter To Continue...\n\n");
}

void cmd ()
{
    ldown(100);
    system("toilet -f mono12 -F metal S-MODE");
    ldown(4);
    printf("[1] Emergency Reboot\n");
    printf("[2] Clean All Progress\n");
    printf("[3] Change Into Pure Net\n");
    printf("[4] Change Into Normal Net\n");
    printf("[5] Edit This Script\n");
    printf("[6] Rebuild This Script\n");
    printf("[7] Delet This Script\n");
    printf("[8] Exit S-MODE\n\n");
    printf("Now Please Type Your Command [1~8]:\n");
    int cmdn;
    cmdn = 8;
    scanf("%d",&cmdn);
    ldown(4);
    switch (cmdn)
    {
        case 1:
            printf("Checking Root Permision...\n\n");
            system("reboot");
            printf("\n\nShutting Down The System...\n\n");
            conti();
            break;
        case 2:
            printf("Cleaning All Progress...\n\n");
            system("kill -9 -1");
            conti();
            break;
        case 3:
            system("pure");
            conti();
            break;
        case 4:
            system("unpure");
            conti();
            break;
        case 5:
            printf("Checking Root Permision...\n\n");
            system("sudo vim /bin/engage.c");
            break;
        case 6:
            printf("Checking Root Permision...\n\n");
            system("sudo gcc /bin/engage.c -o /bin/engage");
            printf("\n\nScript Rebuilt! Please Restart The Script\n");
            conti();
            break;
        case 7:
            printf("Checking Root Permision...\n\n");
            system("sudo rm /bin/engage");
            exit(0);
            break;
        case 8:
            printf("Exiting...");
            ldown(10);
            exit(0);
            break;
        default:
            printf("Unknow Command :(\n\n");
            break;
    }
}

void main()
{
    do
    {
        cmd();
    } while (1==1);
}   

【问题讨论】:

  • 在调用 conti() 之前清除输入缓冲区,在里面,只需使用 getchar()....这就是你所需要的。
  • 系统(“暂停”);应该是正确的方法
  • 不要告诉我们“忽略”一些代码,你应该把它从问题中删除。见minimal reproducible example

标签: c pause unistd.h


【解决方案1】:

你可以这样做:

#include <stdio.h>
int main(){
  puts("Press any key to continue ... ");
  getchar();
}

如果你真的需要让它进入接受:

#include <stdio.h>
int main(){
  puts("Press enter to continue ... ");
  while(getchar()!=27);
}

27 是输入键码。

另一种 C 解决方案是:

system("read -n1 -r -p \"Press any key to continue...\" key")

在 c++ 中它看起来像这样:

#include <iostream>
int main(){
  std::cout << "Press enter to continue ... " << std::endl;
  std::cin.ignore(INT_MAX);
  cin.get();
}

【讨论】:

  • If you really need to make it enter-accepted:...emmm...为什么?你不是问我Press any key to continue ... 吗?
  • @CoderBTS 好的,我已经进行了编辑,现在应该可以了。
  • 但是我尝试了 getch 和 getchar,都不起作用...你可以自己尝试,使用我的源代码。想知道“休息”是否会影响它?
  • @CoderBTS 不,不应该是这样。你可以访问 C++ 吗?
  • @Joel 不完全是。
【解决方案2】:

有几种方法可以做到这一点,您可以将其中一种添加到您的 void conti() 函数中。

getchar

  • 在文件结尾或错误getchar();

在 POSIX 系统中,您可以使用:

#include <unistd.h>

pause ();

在 Windows 中,您可以使用 kbhit()。

kbhit() 函数

  • 用于确定一个键是否被按下。 要在 C 或 C++ 程序中使用 kbhit(),您必须包含头文件“conio.h”。

kbhit() 的示例用法:

while (!kbhit())
    printf("Howdyy you haven't pressed a key.\n");

【讨论】:

  • conio.h 是 Borland C 标准中引入的库,OP 可能无法访问该库。
  • 但我怎样才能从 pause() 重新继续?
【解决方案3】:
#include <conio.h>
#include <stdio.h>
int main(){

  while(1){
    puts("Press command ... ");

  }
}

【讨论】:

  • 感谢您不厌其烦地回答,但请阅读stackoverflow.com/help/formatting,它似乎可以帮助您改进答案。此外,我怀疑无限循环的有用性,它会重复输出,但会输入输入。它与所述问题并不真正匹配。我没有投反对票,但我所描述的可能已经在任何人的脑海中。考虑获得参加旅行的徽章stackoverflow.com/tour
  • 这并没有提供问题的答案。一旦你有足够的reputation,你就可以comment on any post;相反,provide answers that don't require clarification from the asker。 - From Review
  • @cpburnz 我不认为答案是 wrong 是删除的正当理由。毕竟,它确实像 OP 请求的那样“暂停”程序(它也使程序无用的事实是另一个问题)。这正是存在否决票的原因......
猜你喜欢
  • 1970-01-01
  • 2015-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多