473,789 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

piping/reading stdin quirks.

FBSD 4.8/GCC

////////
C-CODE:
////////

char c;

while ( 1 ) {

read(0, &c, 1); printf("%02x ", c);
close(0); open("/dev/stdin", O_RDWR, S_IRUSR|S_IWUSR );
}

//////////
Question:
//////////

this is confusing to me. 'open' doesn't reopen 'stdin'
('errno' keeps getting set, and no new characters are
ever read - after the 1st one).

i have a function that opens, reads, and closes a file.
i would like to use this function to read a file piped
from 'stdin' as well as from disk. the problem is i
can't re-open 'stdin' after the file reading function
closes '/dev/stdin'.

is there any way to "reset" /dev/stdin so that
it functions normally after being close(2)'d?
Nov 13 '05 #1
6 13673
On 10 Oct 2003 13:06:23 -0700, i1*******@yahoo .com (chad kline) wrote:
FBSD 4.8/GCC

////////
C-CODE:
////////

char c;

while ( 1 ) {

read(0, &c, 1); printf("%02x ", c);
close(0); open("/dev/stdin", O_RDWR, S_IRUSR|S_IWUSR );
}

Unfortunately, it is not standard c. Neither open nor close are
defined in the standard library.
<<Remove the del for email>>
Nov 13 '05 #2
i1*******@yahoo .com (chad kline) wrote in message news:<47******* *************** ****@posting.go ogle.com>...
FBSD 4.8/GCC


actually, i have a better example of what i am trying to do.
i have searched "everywhere " for the solution, and have seen
the question asked in many places, but without any clear answers.

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
////////////////////////////////////////////////////
int main(int argc, char* argv[]) {

char c, fd2, err;

fd2 = dup(0);
while ((err = read(0, &c, 1))) { // read pipe.
printf("R: %i:%i:%02x\n", errno, err, c);
} errno=0;
err = dup2(fd2, 0);
printf("2: %i:%i:%02x\n", errno, err, c);
errno=0;
while ((err = read(0, &c, 1))) { // read keyb.
printf("R: %i:%i:%02x\n", errno, err, c);
}

exit(0);}
////////////////////////////////////////////////////

this doesn't work - what needs to be done to make it work?
if i avoid all 'dup's and just continue to read stdin after
reading the pipe, the 2nd read just keeps reading the last
char of the piped file (generating errno's).

i want to make stdin read the keyboard after reading the pipe.
i don't want to use 'streams' (fopen, freopen, etc).
Nov 13 '05 #3
On 10 Oct 2003 19:34:35 -0700, i1*******@yahoo .com (chad kline) wrote
in comp.lang.c:
i1*******@yahoo .com (chad kline) wrote in message news:<47******* *************** ****@posting.go ogle.com>...
FBSD 4.8/GCC


actually, i have a better example of what i am trying to do.
i have searched "everywhere " for the solution, and have seen
the question asked in many places, but without any clear answers.

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

^^^^^^^^^^^^^^^ ^^^^^^

Stop right there, this header is NOT part of C. The minute you
include it, you're off-topic here. Ask in a group that supports your
particular compiler/OS combination.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #4
i1*******@yahoo .com (chad kline) wrote in message news:<47******* *************** ****@posting.go ogle.com>...
FBSD 4.8/GCC


this defines much better the problem
i am trying to solve. it would be
better if the '/dev/tty' stuff
wasn't necessary.

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
/////////////////////////////////////////
int main(int argc, char* argv[]) {

char c; int n, fd;

while ((n = read(0, &c, 1))) // read pipe.
printf("PIPE: %02x:%i\n", c, n);

fd=open("/dev/tty", O_RDWR, 0600);
printf("OPEN: %i:%i\n", errno, fd);

dup2(fd,0); close(fd);

while ((n = read(0, &c, 1)) < 1 ); // wait keyb.
printf("KEYB: %02x:%i\n", c, n);

exit(0);}
/////////////////////////////////////////
Nov 13 '05 #5
i1*******@yahoo .com (chad kline) wrote in message news:<47******* *************** ****@posting.go ogle.com>...
FBSD 4.8/GCC

i apologize for the several posts,
but since it takes 10 hours or so
to post, i was able to figure some
things out, and think it's good
knowledge to have in the DB. it
seems to be a question without
many good answers (not that the
one i found is "good", not sure).

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
///////////////////////////////////////////////////
int main(int argc, char* argv[]) {

char c; int n;

while ((n = read(0, &c, 1))) // read pipe.
printf("PIPE: %02x:%i\n", c, n);

dup2(1,0);

while ((n = read(0, &c, 1)) < 1 ); // wait keyb.
printf("KEYB: %02x:%i\n", c, n);

exit(0);}
///////////////////////////////////////////////////
Nov 13 '05 #6
i1*******@yahoo .com (chad kline) wrote:
i1*******@yaho o.com (chad kline) wrote in message news:<47******* *************** ****@posting.go ogle.com>...
FBSD 4.8/GCC

i apologize for the several posts,
but since it takes 10 hours or so
to post, i was able to figure some
things out, and think it's good
knowledge to have in the DB. it
seems to be a question without
many good answers (not that the
one i found is "good", not sure).

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>


But still it's hopelessly off-topic in comp.lang.c, as you are dealing
with non-standard extensions to the C language.

To echo Jack Klein:
Ask in a group that supports your particular compiler/OS combination.

<SNIP>

Regards
--
Irrwahn
(ir*******@free net.de)
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5853
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child
4
9925
by: Christian Long | last post by:
Hi I'm trying to pipe data into a python program on Windows 2000, on the command line. Like this: dir | myProgram.py Here's what I tried:
2
2675
by: Csaba Henk | last post by:
For me, one reason for using Python is that with the help of it I can rid of several problems that I faced with during writing scripts in Bourneish shells. Biggest of these problem was the quotation madness stemming from the typelessness of the shell language. When using Python as a shell replacement I often launch apps from Python and use different piping constructs to pass data to/get data from these apps. I expect from the language...
7
2764
by: Edward Diener | last post by:
From within a function in my own module I need to take the output from a Python module "A", which is sending data to stdout and which can not be changed and which I need to invoke as a top-level module, and pipe it into another function in my own module so that I can read it from stdin. Is there an easy way to do this ? The only way I can presently think to do this is through "system python A.py | python MyOwnModule.py", which seems a bit...
5
2261
by: runes | last post by:
I trying to figure out a way to make a python script accept data output from another process under Windows XP, but I fail miserably. I have a vague memory having read this is not possible with Python under Windows... But googling for a clue I came across this from /Learning Python/ (Lutz & Ascher) <Learning Python>
2
1876
by: Apple Grew | last post by:
I want to develope a Winboard like application which will support Winboard protocol. For that I require to know how to handle piping in Python. I seperated out module popen2, but when I use fileObject.readline() then it halts the program if the sub-process is waiting for user input; will executing the readline() part in a seperate thread be helpful? My target platform is Windows. Links to helpful web resources and sample Python codes...
1
1772
by: john prokopek | last post by:
I just installed ActiveState Perl 5.8 on a Win2k system. For a sanity check I a one line script to take input from STDIN and print. Hey it doesn't work. If I just perform a print with some text that works. So can someone clue me in. So, why doesn't this print out anything: while(<STDIN>) { print;}
3
7775
by: bernd wegener | last post by:
Hello netties, I coded the following in perl: open(LOGFILE,">&STDOUT") ; if ( $ARGV ) { close(LOGFILE) ; open(LOGFILE,">> $ARGV") or die "Cannot open file $ARGV\n" ;
6
2421
by: mhenry1384 | last post by:
On WinXP, I am doing this nant.exe | python MyFilter.py This command always returns 0 (success) because MyFilter.py always succeeds. MyFilter.py looks like this while 1:
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10408
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9020
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4092
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3700
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.