D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

RE: [LUG] redirects and C

 

Neil,
 
> $ prog < input.file > output.file 
 
This redirection is done by the SHELL, not the program.  This is done by
ksh, bash, csh (whatever) using dup2 after the fork() but before the exec().

 
The program is reading stdin, and there are a number of ways to read stdin
from C.  The simplest is to use fgets (see man pages), #include <stdio.h>
and use stdin for the FILE * (more or less the equivalent of <STDIN> in
Perl).  The considerations are that stdin could come from a tty, a pipe
(anonymous or named) or a file. you can use isatty() to check-out if it is
connected to a tty, but for most programs the differences are irrelavant.
 
Alternatively, use a read from file descriptor (fd) 0 (zero).  This is the
"low-level" Unix way, gives more control (which you might not need) and is
equivalent to sysread in Perl.
 
> Can 'prog' be given options in such a syntax? 

> $ prog --verbose < input.file > output.file 

Yes, since the redirections are stripped off by the shell before the program
gets to see argv.

Cheers,
Clive


________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

<<application/ms-tnef>>