LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   execl question (https://www.linuxquestions.org/questions/programming-9/execl-question-552156/)

kev000 05-08-2007 01:04 AM

execl question
 
In a terminal I can issue the command:
Code:

mplayer file.mp3 > mplayer.output
and it will save all of mplayer's stdout messages to the file.

in my c++ program I try something similar:
Code:

execl("/usr/bin","mplayer","-slave","-idle",">","mplayer.output",NULL);
why doesn't this work the same as a terminal command? How can I send mplayer's output to a file?

wjevans_7d1@yahoo.co 05-08-2007 08:49 AM

It doesn't work because it's the shell (typically bash) which interprets the ">" and sets things up right. With your execl() call, no such interpretation is done; each part of the command line is sent directly to your program, including the ">" and the "mplayer.output".

Speaking of "your program", what is your program? It looks from the execl() call that you're trying to execute "/usr/bin". Your first parameter should be (presumably) "/usr/bin/mplayer"; your second parameter should remain "mplayer", as it is now.

The easiest way out of this is the following C/C++ code:

Code:

#include <stdlib.h>

int result;

result=system("mplayer file.mp3 > mplayer.output");

if(result!=0)
{
  /* Do error handling here. */
}

For more information:

Code:

man execl
man system

Hope this helps.

kev000 05-08-2007 09:36 AM

I wish I could use a system() call in my program. My program is basically a mplayer front-end for use in car PCs. I create a pipe and fork() before I call my execl() so my program can control mplayer via a stdin pipe. The problem has been that I can't seem to create a stdout pipe that works.

I figure I can best access mplayer's output, if it where a file. Any ideas?

wjevans_7d1@yahoo.co 05-09-2007 08:17 AM

I haven't done anything with this, ever, but play around with this on a PC and see if it works for you.

Code:

int fork_result;

fork_result=fork();

if(fork_result<0)
{
  blow up
}

if(fork_result==0)
{
  /* Child code goes here. */

  int execl_result;

  fclose(stdout);

  stdout=fopen("mplayer.output","w");

  if(stdout==NULL)
  {
    blow up
  }

  execl_result=execl(whatever, but leave out the ">" and the file name)

  blow up, because you should never get here
}

/* Parent code goes here. */

If this doesn't work, experiment with functions like dup2() and fdopen().

Hope this helps.


All times are GMT -5. The time now is 12:06 AM.