By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,649 Members | 2,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,649 IT Pros & Developers. It's quick & easy.

problems redirecting an open file to stdin using dup2()

P: n/a
Hello,

I have a module that is part of larger project that is giving me
trouble, so I setup an example.

Brief
=====

I simply want to open a text file and make the contents avaliable
through stdin that way a bourne shell script I wrote will execute
properly. The shell script is expecting input from stdin.

This program will be executing on a UNIX machine and communicating
with the shell.

Code
=====START======

#include <stdio.h>
#include <stdlib.h>

const char cmd[] = "./proj4.sh" ; /* Shell Script */
const char FILE_NAME[] = "tf.txt" ; /* File to open */

int main()
{
FILE *fp ;

/* Open File For Standard Input */
if( ( fp = fopen( FILE_NAME, "r" ) ) == NULL )
{
printf( "Cannot Open File: %s", FILE_NAME ) ;
exit( 1 ) ;
}

if( fp != 0 ) /* If fp isn't stdin */
{
dup2( fp, 0 ); /* Make fp stdin */
close( fp ) ; /* Close original fp */
}

printf( "\n\nRunning Line Now...\n\n" ) ;

execl( "/bin/sh", "sh", "-c", cmd, (char *)0 ) ;
}

=====STOP=======

Essentially the effect I am looking for here is:

../proj4.sh < tf.txt

I cannot get this to execute period. Either I get a compiler error, or
if I change fopen() for open() and make fp an int, the program
executes, but my shell script does nothing, meaning it didn't recieve
anything from stdin.

I would appreciate the help if anyone is will to work with me.

Thank You for your time,

Eli Hayon

Dec 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
DyslexicAnaboko wrote:
Hello,

I have a module that is part of larger project that is giving me
trouble, so I setup an example.

Brief
=====

I simply want to open a text file and make the contents avaliable
through stdin that way a bourne shell script I wrote will execute
properly. The shell script is expecting input from stdin.

This program will be executing on a UNIX machine and communicating
with the shell.
This question is more pertinent to comp.unix.programmer,
comp.os.linux.* etc.

Dec 5 '06 #2

P: n/a
In article <11**********************@n67g2000cwd.googlegroups .com>
DyslexicAnaboko <Dy*************@gmail.comwrote:
>This program will be executing on a UNIX machine and communicating
with the shell.
You want comp.unix.programmer <OT>where we will point out the
fileno() function/macro and perhaps ask for more information
about the version using open()</OT>.

[snippage]
/* Open File For Standard Input */
if( ( fp = fopen( FILE_NAME, "r" ) ) == NULL )
{
printf( "Cannot Open File: %s", FILE_NAME ) ;
exit( 1 ) ;
}

if( fp != 0 ) /* If fp isn't stdin */
This compares fp to NULL. It cannot be equal to NULL, since you
already took care of that case earlier.
{
dup2( fp, 0 ); /* Make fp stdin */
close( fp ) ; /* Close original fp */
}
These are not Standard C functions <OT>and do not take "FILE *"
objects anyway</OT>.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 5 '06 #3

P: n/a
"DyslexicAnaboko" <Dy*************@gmail.comwrote:
# Hello,
#
# I have a module that is part of larger project that is giving me
# trouble, so I setup an example.
#
# Brief
# =====
#
# I simply want to open a text file and make the contents avaliable
# through stdin that way a bourne shell script I wrote will execute
# properly. The shell script is expecting input from stdin.

How about freopen?
freopen("file path","r",stdin)
should close the file associated with stdin and then reopen stdin
associated to the "file path".
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who's leading this mob?
Dec 5 '06 #4

P: n/a

SM Ryan wrote:
"DyslexicAnaboko" <Dy*************@gmail.comwrote:
# Hello,
#
# I have a module that is part of larger project that is giving me
# trouble, so I setup an example.
#
# Brief
# =====
#
# I simply want to open a text file and make the contents avaliable
# through stdin that way a bourne shell script I wrote will execute
# properly. The shell script is expecting input from stdin.

How about freopen?
freopen("file path","r",stdin)
should close the file associated with stdin and then reopen stdin
associated to the "file path".
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who's leading this mob?
I tried frepoen, maybe I used it incorrectly, but I get the same
result, nothing happens.

I am going to repost in the correct group though, sorry about that, it
didn't occur to me that I was posting to the wrong group, thanks so far
though.

Eli

Dec 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.