why standard files?

  • Thread starter Seenivasan Palaniappan
  • Start date
S

Seenivasan Palaniappan

I heard people saying when executing a program 3 standard files will be
opened namely STDIN, STDOUT, STDERR. Is it so? if so what is the
purpose and what each of them stands for?

Explanation of the concept, Guidence to / provision of resources will
be of great help.

Regards,

PL.Seenivasan
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Seenivasan said:
I heard people saying when executing a program 3 standard files will be
opened namely STDIN, STDOUT, STDERR. Is it so? if so what is the
purpose and what each of them stands for?

Explanation of the concept, Guidence to / provision of resources will
be of great help.

"There is a large family of UNIX programs that read some input, perform a
simple transformation on it, and write some output. Examples include grep and
tail to select part of the input, sort to sort it, wc to count it, and so on."
("The Unix Programming Environment" Brian W. Kernighan & Rob Pike)

The designers of Unix decided that there is some utility in providing every
program with predefined access to three data streams. They include access to
an input data stream (called "stdin") that the program can read and process,
an output data stream (called "stdout") that the program can write it's normal
output to, and a second output data stream (called "stderr") that the program
can write error messages or other diagnostics to.

They also provided mechanisms to permit programs to be hooked together in
"pipelines", with the output (that is, stdout) from one program being piped
directly into the input (that is, stdin) of the next program.


- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD5t5OagVFX4UWr64RAo7gAKDKaYXL8voVTEdV6HwhWfIaCg3qQwCeK69W
fbXewYWN9NM1ZkMFtgBVUmE=
=mT6d
-----END PGP SIGNATURE-----
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew said:
"There is a large family of UNIX programs that read some input, perform a
simple transformation on it, and write some output. Examples include grep and
tail to select part of the input, sort to sort it, wc to count it, and so on."
("The Unix Programming Environment" Brian W. Kernighan & Rob Pike)

The designers of Unix decided that there is some utility in providing every
program with predefined access to three data streams. They include access to
an input data stream (called "stdin") that the program can read and process,
an output data stream (called "stdout") that the program can write it's normal
output to, and a second output data stream (called "stderr") that the program
can write error messages or other diagnostics to.

I forgot to add that, originally C was developed to support Unix, and so C
natively supported these three streams. Later, C was standardized such that it
no longer focused on Unix, but the three data streams were still useful enough
a concept to remain unchanged in the definition of the language.

[snip]

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.7 (GNU/Linux)

iD8DBQFD5t7hagVFX4UWr64RAi6AAJ0Ra8BevVEJSexgQozd8s3EyDB2dwCgqN1+
5JSHzzUN5WaerKVtY0x0IVc=
=Fe9D
-----END PGP SIGNATURE-----
 
K

Kenneth Brody

Lew Pitcher wrote:
[... stdin/stdout/stderr ...]
I forgot to add that, originally C was developed to support Unix, and so C
natively supported these three streams. Later, C was standardized such that it
no longer focused on Unix, but the three data streams were still useful enough
a concept to remain unchanged in the definition of the language.

Not to mention the fact that it gives you a portable way to access these
I/O channels if they exist.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
B

Barry Schwarz

I heard people saying when executing a program 3 standard files will be
opened namely STDIN, STDOUT, STDERR. Is it so? if so what is the
purpose and what each of them stands for?

Yes. Your program is free to use them any way it sees fit. Normally
used for input, "normal" output, and error messages, respectively.
Explanation of the concept, Guidence to / provision of resources will
be of great help.

For concepts, look in your text book. The rest is system specific so
you need to look in the documentation of your system.
Remove del for email
 
D

Dave Thompson

Yes. Your program is free to use them any way it sees fit. Normally
used for input, "normal" output, and error messages, respectively.
Almost. You aren't portably allowed to do output on STDIN, or input on
STDOUT and STDERR. They aren't guaranteed to be seekable, and on most
systems much of the time aren't, so you can't rely on that.


- David.Thompson1 at worldnet.att.net
 
R

Richard Bos

Dave Thompson said:
Almost. You aren't portably allowed to do output on STDIN, or input on
STDOUT and STDERR. They aren't guaranteed to be seekable, and on most
systems much of the time aren't, so you can't rely on that.

However, if you _do_ manage to freopen() one of them in read-and-write
mode, you are free to do so. Success of freopen() is checkable.
Unfortunately, doing this to std*, of which you don't (portably) know
the original file name (if they have one at all) requires a null pointer
for the first argument to freopen(), which requires C99.

So yes, your program is free to _try_ and use them as it sees fit - but
you're neither guaranteed to have the means to do so, nor even that
you'll succeed in your attempt if you do.

Richard
 

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
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top