LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2007, 02:04 AM   #1
kev000
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
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?
 
Old 05-08-2007, 09:49 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 05-08-2007, 10:36 AM   #3
kev000
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
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?
 
Old 05-09-2007, 09:17 AM   #4
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help using popen and execl cdog Linux - Software 3 02-28-2007 07:44 PM
fork() + execl() confusion flare Programming 6 01-06-2007 07:21 PM
how can i use execl to replce process rameshtekumudi Programming 5 09-15-2006 05:09 AM
How to get the execl() output? estratos Programming 4 06-07-2006 06:18 PM
execl on a script file iasion Linux - General 1 03-25-2004 07:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration