473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problems redirecting an open file to stdin using dup2()

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
4 6422
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
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 (40°39.22'N, 111°50.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
"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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Schmitt | last post by:
Hello. I wanted to capture the stdout output of a called extension function. Michael Hudson kindly suggested a way to redirect filedescriptors. I still can't get this working: fd_stdout=...
7
by: Dennis Roberts | last post by:
I have a script to parse a dns querylog and generate some statistics. For a 750MB file a perl script using the same methods (splits) can parse the file in 3 minutes. My python script takes 25...
0
by: Katja Filippova | last post by:
hi, Has anybody tried using natural language toolkit (http://nltk.sourceforge.net/) under Python (my version is 2.1.1)? problems: first, I wanted to import a module, as it is written in the...
5
by: Jean-Pierre Bergamin | last post by:
Dear python-Community We are forced to use a quite old simulation software that's based on Modula-2. The idea is now to let this software "talk" to the outside world over a TCP/IP network. ...
3
by: David Douard | last post by:
Hi everybody, let me explain by problem: I am working on an application which consists in a C++ dll (numeric computations) and a Python IHM (Python/Tk), which must run under Linux and win32. My...
2
by: goodnamesalltaken | last post by:
Hello fellow python users, I've been working on a basic implementation of a privilege separated web server, and I've goto the point of running a basic cgi script. Basically when the execCGI...
2
by: seifyr | last post by:
What sequence of calls to open, dup and/or dup2 do you perform in your program in order to achieve the following effects? $prog1>file 2>&1 and $prog2 2>&1 > file I understand which each...
8
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can...
1
by: Lincoln Yeoh | last post by:
Hi, I've just started to learn python (I've been using perl for some years). How do I redirect ALL stderr stuff to syslog, even stderr from external programs that don't explicitly change their...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.