problems redirecting an open file to stdin using dup2()

  • Thread starter DyslexicAnaboko
  • Start date
D

DyslexicAnaboko

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
 
C

Chris Torek

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>.
 
S

santosh

DyslexicAnaboko said:
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.
 
S

SM Ryan

# 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".
 
D

DyslexicAnaboko

SM said:
# 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".

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top