2016-10-06 55 views
0

任何人都可以告诉我如何在shell中执行c程序的输入作为命令?在C中执行用户输入作为shell命令C

我试着用下面的代码

#include <stdio.h> 


    void main() 
    { 
    int year,month; 
    system("date"); 
    printf("Enter the month and year\n"); 
    scanf("%d%d",&month,&year); 
    system("cal &&"); 
    printf("%d %d",month,year); 
    system("ls"); 
    system("ps"); 
    } 

它具有打印由用户提供的年份和月份的压延

谢谢

回答

0

你可以通过用户输入如下打坏;

int year,month; 
system("date"); 
printf("Enter the month and year\n"); 
scanf("%d%d",&month,&year); 

char cmd[4096]; 
sprintf(cmd, "cal -m %d %d", month, year); 
system(cmd); 


printf("%d %d",month,year); 
system("ls"); 
system("ps"); 

人CAL;

-m month 显示指定月份。如果月份被指定为十进制数字,则可能会在后面跟随字母'f'或'p',分别表示该数字的下一个月份或前一个月份。

输出;

... 

enter image description here

... 
+0

合作。谢谢 :) – Nithin