473,387 Members | 1,435 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,387 software developers and data experts.

stdin and stdout

Hello,

I have read about stdin and stdout being standard input and output
streams, but I am still having trouble visualizing how they really
work and what they contain during program execution. For instance
could you use a call to fputc() and/or fseek to write a carriage
return to stdin and cause a subsequent call to getchar() to return
without the user pressing a return key? I have checked the C FAQ and
GNU C Library section on standard streams for more info and found a
lot on their usage, but not much on the internals of how they are
structured and operate. Thanks for any advice.
Nov 13 '05 #1
6 7976
"Andrew" <an***********@ca.com> wrote:
I have read about stdin and stdout being standard input and output
streams, but I am still having trouble visualizing how they really
work and what they contain during program execution. For instance
could you use a call to fputc() and/or fseek to write a carriage
return to stdin and cause a subsequent call to getchar() to return
without the user pressing a return key?
No and no, in that order :) For two reasons. First, all std* streams
are unseekable. Second, stdin is input only, trying to do anything
other than read from it is a UB. (I believe that you can do ungetc
on stdin, but I have been known to be wrong :))

Similarily stdout and stderr are output only.
I have checked the C FAQ and
GNU C Library section on standard streams for more info and found a
lot on their usage, but not much on the internals of how they are
structured and operate. Thanks for any advice.


Consider them ordinary files, except that you do not need to worry
about opening and closing them, as that is done for you automagically.

Peter
Nov 13 '05 #2
Andrew wrote:

Hello,

I have read about stdin and stdout being standard input and output
streams, but I am still having trouble visualizing how they really
work and what they contain during program execution. For instance
could you use a call to fputc() and/or fseek to write a carriage
return to stdin and cause a subsequent call to getchar() to return
without the user pressing a return key? I have checked the C FAQ and
GNU C Library section on standard streams for more info and found a
lot on their usage, but not much on the internals of how they are
structured and operate. Thanks for any advice.


stdin is an input stream, hence output operations are
not meaningful[*]. Dually, stdout and stderr are output
streams, so input operations aren't usable. fseek() and
other positioning functions may or may not work, and this
may vary from one program execution to the next.

The C Standard describes what an implementation may and
must do, but not how it is to be done. P.J. Plauger's "The
Standard C Library" considers many of the issues facing a C
library implementor and exhibits a specimen implementation,
but its internals may have little resemblance to those of
the system(s) you're familiar with.
[*] ungetc() is an "output-like" operation, in a very
limited way. It will do what you're asking for,
but not what I suspect you desire.

--
Er*********@sun.com
Nov 13 '05 #3
an***********@ca.com (Andrew) wrote:
Hello,

I have read about stdin and stdout being standard input and output
streams, but I am still having trouble visualizing how they really
work and what they contain during program execution.
stdin, stdout and stderr are expressions of type 'pointer to FILE'
defined in <stdio.h>. They designate streams opened in text mode,
connected to the console by default on most (?) hosted
implementations. They "contain" user supplied input and program
generated output respectively.
For instance
could you use a call to fputc() and/or fseek to write a carriage
return to stdin and cause a subsequent call to getchar() to return
without the user pressing a return key?
Not so. Write operations on stdin invoke undefined behaviour, as does
fflush(stdin), just in case you're tempted to use this construct.
I have checked the C FAQ and
GNU C Library section on standard streams for more info and found a
lot on their usage, but not much on the internals of how they are
structured and operate. Thanks for any advice.


The internal structure and operation (beyond what is specified for
streams in general) is not subject of the C Standard.

HTH a bit

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #4
"Peter Pichler" <pi*****@pobox.sk> wrote:
[...] all std* streams are unseekable.


Hmm, I think it is implementation-defined behaviour.

<OT>
What about if input is redirected from a file, for example?
</OT>

Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #5
In <al********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
"Peter Pichler" <pi*****@pobox.sk> wrote:
[...] all std* streams are unseekable.


Hmm, I think it is implementation-defined behaviour.


Not even. The implementor cannot guess how I'm going to start the
program ;-) After

prog < input > output

both stdin and stdout are seekable, if the input file exists and the
output file can be created, on both Unix and Windows.

The only thing guaranteed about the std* streams is that they are
text mode streams (unless you freopen them differently).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6
Da*****@cern.ch (Dan Pop) wrote:
In <al********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
"Peter Pichler" <pi*****@pobox.sk> wrote:
[...] all std* streams are unseekable.


Hmm, I think it is implementation-defined behaviour.


Not even. The implementor cannot guess how I'm going to start the
program ;-)


Right, I'll call that invocation-defined behaviour from now on. ;-)

--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #7

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

Similar topics

0
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...
0
by: lickspittle | last post by:
Hi, I have Python embedded with my other code, and when my other code opens a console and redirects stdout, stdin and stderr to it, then calls PyRun_InteractiveLoop, it immediately returns with...
3
by: Harayasu | last post by:
Hi, Using fgets() I can read from stdin and with fputs() I can write to stdout. Now I have two programs, one writing to stdin and the other one reading from stdin. And I would like the second...
23
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard...
6
by: ccdrbrg | last post by:
What is the best way to protect stdin within a library? I am writing a terminal based program that provides plugin capability using the dlopen() API. Sequencing program commands (typed) and...
2
by: velle | last post by:
My headache is growing while playing arround with unicode in Python, please help this novice. I have chosen to divide my problem into a few questions. Python 2.3.4 (#1, Feb 2 2005, 12:11:53) ...
1
by: asdsd sir | last post by:
Hi!I'm new in Python and i'd like to ask some general questions about stdin,stdout... Firstly... if we type like something like : cat "file.txt"|python somefile.py #somefile.py import sys
1
by: BenjaMinster | last post by:
I want to read and write unicode on stdin and stdout. I can't seem to find any way to force sys.stdin.encoding and sys.stdout.encoding to be utf-8, so I've got the following workaround: import...
2
by: kimonp | last post by:
I am running on windows XP with a fresh install of wamp5 (1.7.2) and mediawiki. I am trying to call a perl function from within php using proc_open. The perl script is being executed and...
0
by: Gabriel Genellina | last post by:
En Thu, 25 Sep 2008 09:49:31 -0300, Almar Klein <almar.klein@gmail.com> escribió: Use subprocess.PIPE Usually the tricky part is to figure out exactly whether there is more input or not. With...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.