How to call execl() in C with the proper arguments?

做~自己de王妃 提交于 2019-12-27 14:55:49

问题


i have vlc (program to reproduce videos) if i type in a shell:

/home/vlc "/home/my movies/the movie i want to see.mkv"

it opens up an reproduces the movie.

however, when I run the following program:

#include <unistd.h>

int main(void) {

  execl("/home/vlc", "/home/my movies/the movie i want to see.mkv",NULL);

  return 0;
}

vlc opens up but doesn't reproduce anything. How can I solve this?

Things I tried:

I guessed

execl("/home/vlc", "/home/my movies/the movie i want to see.mkv",NULL);

was equivalent to typing in the shell:

/home/vlc /home/my movies/the movie i want to see.mkv

which doesn't work, so i tried

 execl("/home/vlc", "\"/home/my movies/the movie i want to see.mkv\"",NULL);

and vlc opens up but doesn't reproduce either.

Instead of writing NULL at the end I tried 0, (char*) 0, 1 .... not helpful. Help!!!!


回答1:


execl("/home/vlc", 
  "/home/vlc", "/home/my movies/the movie i want to see.mkv", 
  (char*) NULL);

You need to specify all arguments, included argv[0] which isn't taken from the executable.

Also make sure the final NULL gets cast to char*.

Details are here: http://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html




回答2:


If you need just to execute your VLC playback process and only give control back to your application process when it is done and nothing more complex, then i suppose you can use just:

system("The same thing you type into console");



来源:https://stackoverflow.com/questions/12596839/how-to-call-execl-in-c-with-the-proper-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!