Open In App

CLI programs in C for playing media and shut down the system

Command Line Interface:

How to run it?



Shutdown:

Below is the program to shut down the computer:




// C program to shut down the computer
#include <stdio.h>
#include <stdlib.h>
  
// Driver Code
  
int main(int argc, char* argv[])
{
    char buf[32];
    int time;
  
    // Function converting to integer
    val = atoi(argv[1]);
    sprintf(buf, "C:\\Windows\\System32\\shutdown /s /t %d", time);
  
    // System function to run CLI commands
    system(buf);
  
    // Confirming that program is
    // running properly
    printf("Running successfully. "
           "Shutting down in %d sec",
           time);
  
    return 0;
}

Output:



Explanation:

Note:

Earlier the command C:\\Windows\\System32\\shutdown will be the command resulting in a shutdown. There are various parameters that can be used along with the shutdown command.

shutdown [/i | /l | /s | /sg | /r | /g | /a | /p | /h | /e | /o] [/hybrid] [/fw] [/f] [/m \\computer][/t xxx][/d [p|u:]xx:yy [/c "comment"]]
where /s stands for shut down, /r stands for restart and so on.

  • The /t defines the time-out period before shutdown.
  • By default /t has a value of 30 seconds (if not specified particularly).
  • Say if the programmer wants to shut down the computer after 45 seconds then I have to execute the command shutdown /s /t 45.
  • That is why use the command: C:\\Windows\\System32\\shutdown /s /t and after which the value entered by the user will be added there in the string.
  • So say if through CLI 40 is entered then the final string will look like this:

C:\\Windows\\System32\\shutdown /s /t 40

My Player:

In this program, the task is to make a command-line argument program through which insert a name of the mp3 file, and then the song will be played. So the same concepts are used. There are two ways to open an mp3 file through the command line. They are as follows:

Below is the program to illustrate the My Player program through CLI:




// C program illustrating myplayer
// program through CLI
#include <stdio.h>
#include <stdlib.h>
  
// Driver Code
int main(int argc, char* argv[])
{
    char tbo[100];
  
    // First version of command:
    sprintf(tbo, "G:\\song\\%s.mp3", argv[1]);
  
    printf("The song %s.mp3 is going "
           "to be played in a "
           "second... :",
           argv[1]);
    system(tbo);
  
    return 0;
}

Output:


Article Tags :