Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)


 
 
LinkBack Thread Tools Search this Thread Display Modes
Old 25-03-2005, 10:01 PM   #1 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default help needed reg. batch file.


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
legolas is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-03-2005, 11:26 PM   #2 (permalink)
Wise Old Owl
 
enoonmai's Avatar
 
Join Date: Oct 2004
Location: Parked diagonally in a parallel universe
Posts: 1,304
Default

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.
__________________
Face it, kid! Provoking a reaction isn't the same thing as saying something significant - Calvin
A64 3000+@2.4G/Asus A8V-DLX/1G DDR400/BBA X800 XT PE/320G HGST SATA2
Playing FEAR XP/LSW2
enoonmai is offline  
Old 26-03-2005, 12:26 AM   #3 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default

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
legolas is offline  
Old 26-03-2005, 08:08 AM   #4 (permalink)
Wise Old Owl
 
enoonmai's Avatar
 
Join Date: Oct 2004
Location: Parked diagonally in a parallel universe
Posts: 1,304
Default

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.
__________________
Face it, kid! Provoking a reaction isn't the same thing as saying something significant - Calvin
A64 3000+@2.4G/Asus A8V-DLX/1G DDR400/BBA X800 XT PE/320G HGST SATA2
Playing FEAR XP/LSW2
enoonmai is offline  
Old 26-03-2005, 10:11 AM   #5 (permalink)
Alpha Geek
 
Join Date: Feb 2005
Posts: 959
Default

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

/legolas
legolas is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by topgear
- by abhidev
- by clmlbx
- by Sarath

Advertisement




All times are GMT +5.5. The time now is 05:44 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2