【问题标题】:How to pass a variable in c program as an argument to the batch file?如何将c程序中的变量作为参数传递给批处理文件?
【发布时间】:2011-10-05 21:38:33
【问题描述】:
include<stdio.h>
include<stdlib.h>
  int main()
    {
      char a[20]="hello world";
system("./cool.bat a");\\here I need to pass the array as argument to batch file
       }

我相信你明白我想说的话。我想将 c 程序的数组作为参数传递给批处理文件。但是如果我说

   system("./omnam.bat a") \\ its taking a as an argument

我是怎么做到的?如何将变量或 c 程序数组作为参数发送到批处理文件。假设我在 c 程序中可能有一个整数 I,其值为 15。我如何将它作为参数传递给批处理文件?任何人都可以发布一个带有一些c文件和批处理文件的示例。谢谢

【问题讨论】:

    标签: c batch-file system command-line-arguments argument-passing


    【解决方案1】:

    也许您可以使用标准 IO 编写批处理文件然后执行它?应该一样吧?

    【讨论】:

      【解决方案2】:

      您需要构建一个包含批处理命令的 char[],以及要传递给它的变量内容

      【讨论】:

      • 没明白你能不能解释的更清楚一点?你能帮我写一些代码吗?
      【解决方案3】:

      在运行时使用snprintf构造字符串:

      #include <stdio.h>
      #include <stdlib.h>
      
      int main(void)
      {
          char a[20]="hello world";
          char command[256];
          snprintf(command, sizeof(command), "./cool.bat %s", a);
          system(command);
          return 0;
      }
      

      但是,请记住system 函数非常危险,尤其是当您传递非常量字符串时。出于安全考虑,绝对确保不能将任意用户生成的字符串传入其中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 2010-09-06
        • 1970-01-01
        相关资源
        最近更新 更多