开发者

How to pass a variable in c program as an argument to the batch file?

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
       }

I believe you got what I wanted to say. I want to pass an array of the c program, as an argument to batch file. But if i say

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

How do i make it? How can i send a variable or array of c program as an argument to the batch file. Suppose i might have an integer I in a c program holding a value 15.How can i pass it as an argument to t开发者_开发问答he batch file ? Can anyone please post an example of it with some c file and batch file.Thank you


you'll need to build a char[] consisting of the batch command, and the variable contents to pass to it


Construct the string at runtime using 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;
}

However, keep in mind that the system function is very dangerous, especially when you pass non-constant strings. For security purposes, be absolutely sure that no arbitrary user-generated strings can be passed into it.


Perhaps you can write the batch file using standard IO and then execute it? Should be the same right?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜