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?
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!
Related
say there is a function in my code:
int getwords(int argc, char *argv[])
and I want to call this function in main().
How do I call this in main without erroring out?
`void main(void)
getwords();`
If you write main to provide access to argc and argv:
int main(int argv, char **argv) {
// ...
}
Then you can pass those to another function when you call it:
void foo(int argc, char **argv) {
// ...
}
int main(int argc, char **argv) {
foo(argc, argv);
}
argc will be copied, but argv is a pointer, so the pointer is copied
but not the data it points to.
if for some special reason you do not want to use parameters you can use global variables available in all program.
int gargc;
char **gargv;
int main(int argc, char *argv[])
{
gargc = argc;
gargv = argv;
getopt();
}
int main(int argc, string argv[ ])
{
variable(x);
}
Such as here. How can I use argv and argc here??
int variable(x)
{
//I want to use argc and argv here..
}
Yes, they are just like any other function parameters:
int variable(int x, int argc, char *argv[])
{
for(int i = 0; i < argc; ++i)
printf("%s\n", argv[i]);
return x;
}
int main(int argc, char *argv[])
{
int x = 42;
variable(x, argc, argv);
}
why I am not succeed
int main(char* name,int arg0,int arg1)
{
name = "/u/e2014/Desktop/os/Prog.c";
arg0 = 0;
arg1 = 1;
char my_args[3];
my_args[0] = arg0;
my_args[1] = arg1;
my_args[2] = NULL;
execl(name,m_args);
return(0);
}
I want that my program will execute the program in the path "name".
Right now its do nothing.
I am not understand where is my mistake?
I program in C on linux, and compile it with gcc
Thanks a lot!!
gcc has 3 different signature for main function
int main(void);
int main(int argc, char* argv[]);
int main(int argc, char *argv[], char *envp[]);
Your main function doesn't match either of these. therefore compiler error.
For your case you can use the 2nd signature with a small modification.
#include <stdlib.h>
int main(int argc, char **argv)
{
char *path;
int int1, int2;
path = argv[1];
int1 = atoi(argv[2]);
int2 = atoi(argv[3]);
}
First you are passing wrong parameter in int main(). main() has at least 3 args only.
int main(int argc, char*argv[], char *envp[]);
To execute your program you should use execvp() because you passing arrey of char* not command-line arguments via a variable-argument.
difference between execl and execv?
**L vs V: whether you want to pass the parameters to the exec'ed program as
L: individual parameters in the call (variable argument list): execl(), execle(), execlp(), and execlpe()
V: as an array of char* execv(), execve(), execvp(), and execvpe()**
#include <stdio.h>
#include <unistd.h>
int main(int argc,char*argv[])
{
char *name = "/root/a.out";
char *arg0 = "0";
char *arg1 = "1";
char *my_args[4];
my_args[0] = name;
my_args[1] = arg0;
my_args[2] = arg1;
my_args[3] = NULL;
execvp(my_args[0],my_args);
return(0);
}
So, I have been working on this simple block of code. I would like it to print, when I type in "./a.out -n"
However, that is not working. I have been on stackoverflow trying to work on this, but no such luck. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
void parse_cmdline(int argc, char *argv);
int main (int argc, char *argv[]) {
parse_cmdline(argc, argv);
}
void parse_cmdline(int argc, char *argv)
{
int x,i,m,n = 0;
if (*(++argv) == 'n'){
x = 1;
printf("Output array: "); /* not being displayed*/
}
}
Just write
if (**++argv == 'n'){
And the function should be declared like
void parse_cmdline(int argc, char **argv);
Otherwise you should specify what parameter you are going tp pass to the function. For example
parse_cmdline(argc, argv[1]);
You can check what parameters are passed to the program the following way
int main (int argc, char *argv[]) {
for ( int i = 0; i < argc; i++ ) puts( argv[i] );
}
Try:
#include <stdio.h>
#include <stdlib.h>
void parse_cmdline(int argc, char *argv[]);
int main (int argc, char *argv[]) {
parse_cmdline(argc, argv);
}
void parse_cmdline(int argc, char *argv[])
{
int x,i,m,n = 0;
if (*(argv[1]) == '-' && *(++argv[1]) == 'n'){
x = 1;
printf("Output array: "); /* not being displayed*/
}
}
And run it with ./a.out -n.
So, I have examined 0th and 1th character of the argv value on the position one ("./a.out" is located on position 0, and "-n" on position 1).
Is that what you wanted?
Also, you cannot ignore warnings:
1.c: In function ‘main’:
1.c:5:23: warning: passing argument 2 of ‘parse_cmdline’ from incompatible pointer type [-Wincompatible-pointer-types]
parse_cmdline(argc, argv);
^
1.c:3:6: note: expected ‘char *’ but argument is of type ‘char **’
void parse_cmdline(int argc, char *argv);
If you write
parse_cmdline(argc, argv);
then parse_cmdline should be
void parse_cmdline(int argc, char *argv[]);
I want to call Program1 from Program2 with exact same parameters which I called Program2 with.
In Linux, I can do it like this:
int main(char argc, char* argv[]){
execv("./Program1", argv);
}
In windows, I tried CreateProcess
but as the first post says there is potential issue: "argv[0] Doesn't Contain the Module Name as Expected". I do want to send proper argv[0] to Program1. What should I do?
argv[0] is the name of the program itself.
You should do :
int main(char argc, char **argv)
{
char* argvForProgram1[] = { "./Program1", 0 }
execv(argvForProgram1[0], argvForProgram1);
}
or to keep your previous args :
int main(char argc, char **argv)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execv(argvForProgram1[0], argvForProgram1);
}
Using execve is better too because you keep the environment:
int main(char argc, char **argv, char **envp)
{
char** argvForProgram1 = argv;
argvForProgram1[0] = "./Program1";
execve(argvForProgram1[0], argvForProgram1, envp);
}