help needed reg. batch file.

Status
Not open for further replies.

legolas

Padawan
hi,

i actually have created a program which has some arguments and does some calculations and writes the o/p on a text file. i compiled the code and executed it within C to get the EXE file. now i wanted to write a "batch file, *.bat" which could call this exe file with the arguments i specify as input and do the calculations necessary... instead of me clicking on the exe file everytime. my code was simply

@echooff
call <exefile name> <argument1> <argument 2> etc...

is this right? if not pls help me with this or pls redirect me to a C forum which could help me.... thanks in advance...

/legolas
 

enoonmai

Cyborg Agent
Say your program is example.exe and it takes arguments 10 and 20. Your batch file should look like this:
Code:
@ECHO OFF
EXAMPLE.EXE 10 20
@ECHO ON

If you want to supply arguments dynamically at run time, that is supplying the arguments at the command line like this
example.exe 10 20
and the next time with
example.exe 45 60
then you cant go around editing the batch file over and over again each time. In that case the batch file should be:

Code:
@ECHO OFF
EXAMPLE.EXE %1 %2
@ECHO ON

But if you want to use this the right way, then you can have your C programs accept multiple arguments with command line arguments/varargs. If you want help with those, please post back, else these batch files should do the job.
 
OP
L

legolas

Padawan
thanks for that quick reply. :) i ll try this one first.. however, i ll also like to learn that other one reg. the arguments u talked abt.. if u could spare ur time to tell methat... it would be useful! :) thanks again..

/legolas
 

enoonmai

Cyborg Agent
Its pretty easy, command line arguments program can be run in the same way as I talked about with the batch files. In fact, for the previously mentioned batch files to even work, you would need command line arguments supported by your program, else your program would treat
example.exe 10 20
in the same fashion as
example.exe

A simple command line argument - compatible program would look like this:

Code:
#include <stdio.h>
void main(int argc, char *argv[]) { /*Note the arguments for the main() function) */
int a=0, b=0, c=0;
a=argv[0]; b=argv[1];
c=a+b;
printf("\nThe sum is %d", c);
}

When you run the above program, called example.c, compiled into example.exe in the following way

example 10 20
it would output
"The sum is 30"

Run it with the "example 20 30" and it would output "The sum is 50" and so on. There are no error handlers in the above program so it doesnt check if you have entered the correct numbers/types of arguments. Remember that argc holds the count of the number of arguments, while the argv holds the actual arguments themselves. Also, remember that since argv[] is an array, it starts with an index value of 0, so argv[0] is the first argument.

When it comes to varargs, well the best example of a function that supports varargs is printf() as you can supply any number of arguments to it and still have it decode and work accordingly. Any function can be made to accept varargs with the ellipsis operator, consisting of the three dots (...) Consider this example:

Code:
#include <stdio.h>
#include <stdarg.h>
void str_printer(const char *start, ...) {
va_list arglist;
int i; char *s;
va_start(arglist, start);
while(s) {
s=va_arg(arglist, char*);
i=va_arg(arglist, int);
if(s)
printf("%s %d\n", s, i);
}
va_end(va_list);
}

void main() {
str_printer("Run1", "Argument1", "Argument2");
str_printer("Run2", "Argument1a", "Argument2a", "Argument3", "Argument4", "AAARGGGH!");
}

When you run this program the output would be:
Code:
Run1 Argument1 Argument2
Run2 Argument1a Argument2a Argument3 Argument4 AAARGGGH!

The parameters are not known to the function until runtime, so it uses the macros from stdarg.h to access them. The program defines a variable arglist of type va_list, which is defined in stdarg.h as an array for obtaining the arguments that come in place of the eliipsis ...
The macro va_start initializes the va_list variable (arglist, which is the first parameter for the macro) and the second parameter is the name of the first parameter accepted by our function str_printer, which is start.
The macro va_arg actually extracts the arguments. So, when its run with s=va_arg(arglist, char*); it knows to expect an argument of type char* and the next va_arg statement is told to expect an int and so on and so forth. The macro va_end cleans it all up and releases memory. Hope this would have explained it a bit.
 
OP
L

legolas

Padawan
gr8! i would like to try that second one now instead! thanks for those detailed explanations.. will revert back soon.. thanks a lot! :)

/legolas
 
Status
Not open for further replies.
Top Bottom