Search code examples
cpauseunistd.h

How to pause the C program?


Fixed

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

I am now writing a command line tool and using C language, but I am wondering how to pause the program? (Not abort it, but can be re-continued) I was trying to make it like "Press Enter To Continue".

I am on Linux so I don't have or what, I have , and I tried "pause()", but this function cannot realize "Press Enter To Continue", it cannot be re-continued.

Wonder if there is a function or what can help me to do that? If yes, please tell me, thanks!

PS: These are my codes above, as you may see, I am using a function named "conti" to set a pause

PLUS: Ignore those "delay" functions, just doing tests.

#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);
}   

Solution

  • You can do it like that:

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

    If you really need to make it enter-accepted:

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

    27 is enter keycode.

    Another C solution would be:

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

    In c++ it would look like that:

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