传递变量系统功能用C [英] Passing variables to system function in C

查看:120
本文介绍了传递变量系统功能用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个C code:

I have this C code:

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

int main()
{
    int a;

    printf("Please enter a number:\n");
    scanf("%d",&a);
    printf("Your number is: %d\n",a);
    system("echo %d",a);
}

我感兴趣的最后一个命令中,系统()功能,为什么我不能打印我的变量就像我与打印了的printf()。我希望能够要求用户输入一些,比方说一个字符串,然后把它传递给系统的功能。

I'm interested in the last command, the system() function and why I can't print my variable like i printed it with printf(). I want to be able to ask the user some input , let's say a string and then pass it to the system function.

实例:

要求用户提供的文件夹名称

Ask user for folder name

system("mkdir %s", FolderName);

感谢你在前进! :)

推荐答案

使用的snprintf

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

int main()                                                                     
{                                                                              
    int a;                                                                     
    char buf[BUFSIZ];                                                          

    printf("Please enter a number:\n");                                        
    scanf("%d",&a);                                                            
    printf("Your number is: %d\n",a);                                          
    snprintf(buf, sizeof(buf), "echo %d",a);                                   
    system(buf);                                                               
}  

这篇关于传递变量系统功能用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆