C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-05-2009, 08:03 AM   #1
Registered User
 
Join Date: Jun 2009
Posts: 9
Unhappy Executing another program, redirecting its output to a file, and returning control.

Okay, so basically I'm trying to write a program that I can use to convert video files and transfer them to my mp3 player.

The basic part worked fine initially, using system(), I would call ffmpeg, have it convert the file, then use system again to call mtp-sendtr. The problem is, I need to specify the length of the video in seconds to mtp-sendtr. I can check the length myself and input it manually, and that works, but since ffmpeg displays the length as it converts, it seems I could use this data somehow.

First, I tried simply piping the output from ffmpeg to a text file like "ffmpeg blah blah blah > output.txt" but that doesn't work, the file ends up being blank.

So I did this (I've replaced ffmpeg with ls just to simplify things):

Code:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>


int main(int argc, char *argv)
{
int fd; /*file descriptor to the file we will redirect ls's output*/

if((fd = open("dirlist.txt", O_RDWR | O_CREAT))==-1){ /*open the file */
  perror("open");
  return 1;
}


dup2(fd,STDOUT_FILENO); /*copy the file descriptor fd into standard output*/

dup2(fd,STDERR_FILENO); /* same, for the standard error */


close(fd); /* close the file descriptor as we don't need it more  */

/*execl ls */
 execl("/bin/ls", "ls", (char *) 0);

printf("All done.\n");

return 0;
}
That works, of course, but then the program just ends. I don't know much about forks and dup2 and all that, and I'm trying to learn, but I can't find anything that seems to answer my questions.

I have a feeling I'm going about this in a convoluted way, and I'd LOVE to have someone explain to me what I need to do to make this work.
IanKoro is offline   Reply With Quote
Old 06-05-2009, 08:14 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Read up on popen(), it will be easier for what you are trying to do, vis tapping the output from ffmpeg.

Quote:
Originally Posted by IanKoro View Post
I have a feeling I'm going about this in a convoluted way, and I'd LOVE to have someone explain to me what I need to do to make this work.
Yes, no matter how you slice it using C to do this is convoluted. It would be better done in a shell script, but if you are more comfortable programming in C than for bash you might as well try.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 06-05-2009, 08:19 AM   #3
Registered User
 
Join Date: Dec 2006
Posts: 1,780
You need to fork somewhere.

execl() replaces the process image, so it will never return.

You need to first create pipes (using pipe()) for I/O, and then fork. In the child process, dup2() stdin and stdout to 2 ends of the pipes, close the unneeded ends, and call exec(). In the parent, close the unneeded ends (should be the opposite ends to the ones closed by the child), and then you can read/write from/to the pipes.

I have recently written a C++ wrapper for this. You may want to have a look.
Code review for a class that spawns a new process and allows IPC
(final code on the last post)
The AppInstance::start() function in particular.

OR, if you only need to read OR write to the child process (as opposed to both read AND write), you can just use popen().

As for shell redirection not working, maybe it writes to stderr? (People do that because stderr is usually not buffered, whereas stdout usually is)
cyberfish is offline   Reply With Quote
Old 06-05-2009, 08:22 AM   #4
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Quote:
Originally Posted by cyberfish View Post
You need to fork somewhere.

execl() replaces the process image, so it will never return.

You need to first create pipes (using pipe()) for I/O, and then fork. In the child process, dup2() stdin and stdout to 2 ends of the pipes, close the unneeded ends, and call exec().
This is what popen() does and it does it properly. Just use popen().
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 06-05-2009, 08:34 AM   #5
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Life would be so good if popen supports "rw" mode...

But yes, for this application, popen() is easier, since you presumably don't need to write to ffmpeg.
cyberfish is offline   Reply With Quote
Old 06-05-2009, 09:41 AM   #6
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> Life would be so good if popen supports "rw" mode...
IIRC, some versions do (or at least I recall someone saying they do)
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 06-05-2009, 09:42 AM   #7
Registered User
 
Join Date: Jun 2009
Posts: 9
Ahh... well, I figured out how to fork it, thank you very much... I found an example that cleared things up tremendously.

I know C is a convoluted way of doing this, but I'm mostly trying to teach myself stuff.
IanKoro is offline   Reply With Quote
Reply

Tags
dup2, execl, fork, redirect

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fopen returning "Bad File Pointer" jprukop C Programming 4 04-17-2009 03:01 PM
Problems passing a file pointer to functions smitchell C Programming 4 09-30-2008 02:29 PM
Problems with returning a char* to fprint. qubit67 C Programming 5 08-08-2007 11:28 PM
Why does this program not work on my laptop?! Eddie K C Programming 1 03-11-2006 04:34 PM
Dikumud maxorator C++ Programming 1 10-01-2005 06:39 AM


All times are GMT -6. The time now is 08:29 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22