Reputation: 100
Let's say I have a file test.c
which accepts a text file and starts as follows:
int main(int argc, char **argv)
{
...
return 0;
}
I will typically compile the file and execute the binary as follows: ./test input.txt
.
Now, I wish to call the main
function programmatically in another function in the same file. How should I proceed?
Upvotes: 0
Views: 1769
Reputation: 2853
You can do this as follows:
#include <stdio.h>
int main(int argc, char *argv[]);
void func()
{
char *argv[] = { "./test", "another.txt", NULL };
main(2, argv);
}
int main(int argc, char *argv[])
{
if (argc > 1) {
printf("Processing %s...\n", argv[1]);
}
/* ... */
func();
return 0;
}
This should output something like:
Processing input.txt...
Processing another.txt...
Beware of endless recursion!
Upvotes: 3