ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
in my class we always use execl, but our lecturer throw a question to us
: he want us to write a c program that can prompt the user to key in the executable file's path name,file name,arguments.
a example for execl:
execl("/bin/ls","ls","-l",0)
but, obviously, execl cannot fullfil the question. after a long time for me to search to solution, i found execv is a good method.
for example:
char *av[]={"ls","-1",0};
execl("/bin/ls",av);
from the example above i found a idea for my problem:
----------------------------------------------------------------
char pathName[20];//for path name string
scanf("%s",pathName);
int argumentNumber;
scanf("%d",&argumentNumber);// ask the user for number of arguments
char av[argumentNumber][20];//create a 2 dimensional array to store
//the arguments
int i;
for(i=0;i<argumentNumber;i++)//use for loop to store the arguments
scanf("%s",av[i]);
execv(pathName,av);
------------------------------------------------------------------
please forgive me to ignore the prompting message.
unfortunely, this program is not working.the system call function execv keep failed. this is really disappointed me.
i thought the problem is the NULL character in the execv function, so i add the '\0' into the last position:
*av[argumentNumber]='\0';
but execv still failed to execute.
moreover i tried to display the av array by using for loop, and the string inside is perfectly exist.
really hope someone can help me solve this problem.
really appreciated.
i admitted this is my homework. but i spend a lot of time still cannot figure out where is my problem.
Moreover i feel that i almost overcome the problem but still has a small things stuck me. The only way i can get
help from is internet, but i search quite a lot of website, they only introdue the usage of exec family simply but not deeply.
You know why i dont want to ask my lecturer? very funny is he also does not know where is my problem.
execv(pathName,av);
the argument pathName work corrently, but only that two dimesional array av work incorrectly.
this is what i found from internet: int execv(const char *path, char *const argv[]);
my program is supposed to work.......
if someone cannot give me the answer directly, please give me a hint or a direction to let me can continue my problem.
thanks again.
You know why i dont want to ask my lecturer? very funny is he also does not know where is my problem.
Really? Seems like your lecturer doesn't know the material very well, you have (at least) 4 problems.
Quote:
Originally Posted by sam1201
i thought the problem is the NULL character in the execv function, so i add the '\0' into the last position:
*av[argumentNumber]='\0';
This statement is writing past the end of the array.
The execv function doesn't need a NULL character in the last position, it needs a NULL pointer.
You didn't include the header for execv (unistd.h)
If you had included that header the compiler would have warned you have a type mismatch, a 2D array of char's is not the same as an array of char pointers.
really thanks for your reply. that is really helpful.
haha, so sorry i wrote wrongly the
*av[argumentNumber-1]='\0'; to *av[argumentNumber]='\0';
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
finally i think my problem is my 2D dimensional array:
char array[10][10]
only can pass to function(array), when the function is : void function(char [][column]), but execv is int execv(const char *path, char *const argv[]);
i thought the only way for let user to input string is two dimenional array:
char array[10][10];
because it specify the size, instead of declare like char *array[10], no doubt this will give run time error because unspecified size.
but now i found a way
char * array[10];
char str[10];
so i can let user key in the string and store in: scanf("%s",str);
after that i do this: array[0]=str; hahaha, the effect is same with two dimensional array. the size is also 10x10, but i can pass to execv as a pointer.
i think this time it is supposed to work
unfortunely, my ubuntu dont know why cannot compile c program, so i need to go to school lab to try my program, and the free lab cannot connect to my samba!!! i need to retype everytime. god bless me tomorow my program will be working. haha
in addition, why i did't include <unistd.h>, but no error for my execv?
and i pass the argument wrongly also no compile error, like this execv(pathName,argumentList) //argumentList is two dimenional array
but once i include <unistd.h>, my error appear xD ..
i think this time it is supposed to work
unfortunely, my ubuntu dont know why cannot compile c program
You didn't install gcc? apt-get install build-essential should fix that.
Quote:
in addition, why i did't include <unistd.h>, but no error for my execv?
and i pass the argument wrongly also no compile error, like this execv(pathName,argumentList) //argumentList is two dimenional array
but once i include <unistd.h>, my error appear xD ..
That's just the way C is. If you compile with the -Wall option then gcc will give you a warning about missing prototypes.
if a loop happen, all the array[i] will point to str and cause all array[i] same value because all point to same place = =. i forgot '=' in string is not copy of value......
luckily i just learn malloc() from internet, that is really a good function
argumentList[i]=malloc( sizeof(char)*20 );
so i don't need str anymore, i can directly use scanf("%s",argumentList[i]) in a for loop. but i need use malloc() everytime before scanf
who know how to pass a two dimensional array to a pointer-to-array type of parameter?
for example:
char array[10][10];
function(???); // function(array) sure is not correct, i tried before.
[QUOTE=ntubski;3621770]You didn't install gcc? apt-get install build-essential should fix that.
yaya i tried that before, but i cannot connect to internet...
actually i do have wireless network in my school, but i am not sure how to use ubuntu link to wireless network. i follow the way that the internet teach to detect the wireless signal but i failed.
is that like this?
system->administration->network
i need connection because of this command:
sudo apt-get update
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.