简体   繁体   中英

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 the 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM