Reputation: 20355
I have a Visual Studio C++ project. If I want to only run it once, I can simply manually input the arguments of
int main(int argc, char** argv)
into debugging options
and then click Debug.
However now, I wish to run multiple times with different parameters in one go. e.g. I wish to run it like this:
for(i=0; i<10; i++)
{
main(i);
}
How may I do this with Visual Studio?
Upvotes: 0
Views: 1835
Reputation: 881653
I don't believe it's technically legal (per the standard) to call main
. Even if it were, you'd be passing the wrong parameters (argc == 1
and argv == ${DEITY}_ONLY_KNOWS
).
This is a problem I would perhaps solve with a cmd script. Just make sure that your program is compiled, then run something like:
@echo off
cd \path\to\debug\dir
rem this bit can be as complex as cmd.exe allows:
for /l %%a in (0,1,50) do (
for /l %%b in (0,1,10) do (
echo data.txt result.txt %%a %%b
)
)
This will run a command (echo
in my case so you can see it working but you should replace that with your actual executable name, and modify the cd command to select the proper directory) 561 times (51 x 11) with the first two arguments fixed and the last two running 0-50 and 0-10, the output of which finishes:
: : : : : : : : : :
data.txt result.txt 49 7
data.txt result.txt 49 8
data.txt result.txt 49 9
data.txt result.txt 49 10
data.txt result.txt 50 0
data.txt result.txt 50 1
data.txt result.txt 50 2
data.txt result.txt 50 3
data.txt result.txt 50 4
data.txt result.txt 50 5
data.txt result.txt 50 6
data.txt result.txt 50 7
data.txt result.txt 50 8
data.txt result.txt 50 9
data.txt result.txt 50 10
There may be a way to get VS to do this automatically as part of a build/run sequence but I'm not aware of any (and I tend to opt for the simplest solutions most of the time).
You could, of course, refactor your code so that you rename main
to something else then have a filter main
as follows:
int main (int argc, char *argv[]) {
// Do this normally:
return worker (argc, argv);
// Or do this for debugging:
// int stat = 0;
// for (int i = 0; i < 10; i++) {
// // construct argv-lookalike based on i.
// stat = worker (myargc, myargv);
// if (stat != 0) break;
// }
// return stat;
However, the construction of an argv-array is not trivial since you have to follow the same rules as laid out in the standard, specifically that argc
and argv
must agree, argv
must an array of character pointers (one more than is indicated by argc
) so that each argument is a C-style string, argv[0]
must represent the program name, argv[argc]
must be NULL, and so on.
It's probably easiest to use the cmd
script solution and let the startup code take care of all that argument preparation for you.
Upvotes: 3
Reputation: 177765
Let's say you have this hypothetical program:
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
if(argc != 2)
{
cerr << "bad input" << endl;
exit(1);
}
auto arg = atoi(argv[1]);
cout << (2*arg) << endl;
}
You could use a batch script. Save the following as test.bat
in the same directory as your program executable. Type help
or specifically help for
in a command prompt for more information about batch file commands:
@echo off
for /L %%i in (0,1,9) do prog.exe %%i
Output:
0
2
4
6
8
10
12
14
16
18
Or you could refactor main to loop and call a function:
#include <iostream>
using namespace std;
void func(int arg)
{
cout << (2*arg) << endl;
}
int main()
{
for(auto i = 0; i < 10; ++i)
func(i);
}
Output:
0
2
4
6
8
10
12
14
16
18
Upvotes: 2
Reputation: 343
In C++ it is illegal to call main
within a program (C++11 §3.6.1/3). However, any work that is done inside main
can easily be done in another function.
int main( int argc, char** argv ) {
foo( argc, argv );
}
int foo( int argc, char** argv ) {
// do something
}
At this point, feel free to call foo(...)
as many times from within your program (or Visual Studio debugger) as you want.
Upvotes: 4