The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Redirecting OUTPUT Chanakya.m Shell Programming and Scripting 1 08-13-2007 04:27 PM
redirecting the output of aspell leekb UNIX for Advanced & Expert Users 6 07-10-2006 05:50 AM
Redirecting the startup output blakmk UNIX for Dummies Questions & Answers 3 10-28-2003 07:22 AM
Redirecting output to TCP port mscomms UNIX for Dummies Questions & Answers 1 09-30-2003 10:51 PM
Redirecting output to multiple log files? darthur UNIX for Dummies Questions & Answers 3 01-15-2002 12:54 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2008
JamesGoh JamesGoh is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 89
execl() + redirecting output to text files

Im currently using execl() to run the ls command and redirect the output to a text file. Unfortunately, when I check the text file, no information has been written to it.

I am calling execl() with the ls command like this

Code:
execl( "/bin/ls" , "-al" , '>' , "dirlist.txt" ,(char *) 0 );
What are some possible causes of my above problem ? cheers
  #2 (permalink)  
Old 02-13-2008
HPAVC's Avatar
HPAVC HPAVC is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 106
Is that the right syntax for execl?

Quote:
Originally Posted by man execl
execl(<shell path>, arg0, file, arg1, ..., (char *)0);

where <shell path> is an unspecified pathname for the sh utility, file is the process image file, and for execvp(), where arg0, arg1, and so on correspond to the values passed to execvp() in argv[0], argv[1], and so on.

The arguments represented by arg0,... are pointers to null-terminated character strings. These strings shall constitute the argument list available to the new process image. The list is terminated by a null pointer. The argument arg0 should point to a filename that is associated with the process being started by one of the exec functions.

The argument argv is an array of character pointers to null-terminated strings. The application shall ensure that the last member of this array is a null pointer. These strings shall constitute the argument list available to the new process image. The value in argv[0] should point to a filename that is associated with the process being started by one of the exec functions.

The argument envp is an array of character pointers to null-terminated strings. These strings shall constitute the environment for the new process image. The envp array is terminated by a null pointer.

For those forms not containing an envp pointer ( execl(), execv(), execlp(), and execvp()), the environment for the new process image shall be taken from the external variable environ in the calling process.

The number of bytes available for the new process' combined argument and environment lists is {ARG_MAX}. It is implementation-defined whether null terminators, pointers, and/or any alignment bytes are included in this total.


  #3 (permalink)  
Old 02-16-2008
pflynn pflynn is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 11
Quote:
Originally Posted by JamesGoh View Post
What are some possible causes of my above problem ? cheers
The problem is: you are giving ls something that actually is the shell's responsability, and that thing is output redirection. Since there is no shell involved in your program, you should perform the redirection of the output by yourself, that is, your program should do the shells's work, first by openning a new file and them redirecting its standard output and standard error to the new file descriptor using the dup2(2) function.


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


int main(int argcchar *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" "-la" , (char *) );

return 
0;

Hope you got the point
  #4 (permalink)  
Old 02-17-2008
JamesGoh JamesGoh is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 89
Hi guys

I ended up using system() instead and it resolved my probs (although I had to use a lot of strcmp() calls to create the command ) of redirecting shell output to a file. Another reason why I chose to reject execl() in favour of system() was because I didn't really understand the manual description of it too well (apart from its purpose of course)

pflynn, thanks for your suggestion. Just a question (which may be a bit stupid so bear with me). The standard output is the place where all output of any c program is directed to (i.e. the place where all printf() calls have the text displayed ) ?
  #5 (permalink)  
Old 02-17-2008
pflynn pflynn is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 11
Quote:
Originally Posted by JamesGoh View Post
The standard output is the place where all output of any c program is directed to (i.e. the place where all printf() calls have the text displayed ) ?
Yes, you are right!

Quote:
Standard output (stdout)

Standard output is the stream where a program writes its output data. The program requests data transfer with the write operation. Not all programs generate output. For example the file rename command (variously called mv, move, ren) is silent on success.

Unless redirected, standard output is the text terminal which initiated the program.

The file descriptor for standard output is 1 (one); the corresponding <stdio.h> variable is FILE* stdout; similarly, the <iostream> variable is std::cout.
Pay attention to this part:

Quote:
Unless redirected, standard output is the text terminal which initiated the program.
which is exactly what you want to do: you want to open a file so the ls program would write its output to this file instead of writing it to the terminal that's originally associated to the standard output.

More information on the standard streams:

Standard streams
Standard streams - Wikipedia, the free encyclopedia
  #6 (permalink)  
Old 02-18-2008
JamesGoh JamesGoh is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 89
thanks heaps pflynn !
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0