LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-06-2006, 03:44 AM   #1
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
How to get the execl() output?


I need to get the output from an execl() or a system() call. How can I do it? Is there any way of redirecting the output to a text file? Any ideas?

Thanks,

Daniel.
 
Old 06-06-2006, 06:18 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

If you want to write to a text file, just pipe it, e.g.
system("ls >/tmp/ls.txt");

If you want to read the output in your program, you can use popen and pclose which acts a bit like fopen and fclose, e.g.
FILE fp = popen("ls","r");
Then read from fp as it was a regular file, and when you're done, use pclose instead of fclose.

Hope this helps,
Guttorm
 
Old 06-06-2006, 01:23 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Use pipes cause they are faster since it doesn't have to deal with the disk.
Code:
int pfd[2];
pipe(pfd);
pid_t pid=fork();
if(pid==-1)
{
   perror("fork");
   exit(-1);
}
else if(pid==0)
{
   close(pfd[0]);
   dup2(pfd[1],STDOUT_FILENO)
   close(pfd[1]);
   execl("foo","foo",...);
   perror("exec");
   exit(-1);
}
waitpid(pid,0,0);
close(pfd[0]);
 
Old 06-07-2006, 01:23 AM   #4
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
And what about using freopen()?

FILE* fpStdout = freopen(STDOUTFILE, "w+", stdout);
system("ls");

Then I can access fpStdout with fgets, as any other file.
Is this method slower than those proposed by you? Does freopen use pipes too?

Thanks,

Daniel
 
Old 06-07-2006, 05:18 PM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
That redefines stdout in such a way that it cannot be recovered as true stdout later, as well as involving the hard disk when you probably don't need to.

popen and pclose use pipes and will work, but it may not have the level of control you may need/want.
 
  


Reply



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
system() and execl anyone got any sample code please? twirl Programming 2 09-08-2005 02:01 AM
How to use execl, man is not helping grupoapunte Programming 2 05-29-2005 07:27 PM
execl: couldn't exec `/bin/sh' Daredevil Linux - Newbie 2 04-12-2004 11:06 AM
execl on a script file iasion Linux - General 1 03-25-2004 06:40 AM
Can I put execl command output into a file? CrazyDragon Programming 1 12-17-2003 04:34 AM

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

All times are GMT -5. The time now is 03:56 AM.

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