Using fgets w/ stdout

A

akins.steve

Is it possible to write a function that uses stdout for both output
and input? What I mean to say is, I can obviously write a line to
stdout to ask for input, ie:

fprintf(stdout, "%s\n", prompt);

Then in order to read the response, I would use:

fgets(user_input, MAX_LINES, stdin);

Is that the correct way to do it? Is there ever an instance where
this would work:

fgets(user_input, MAX_LINES, stdout);

Thanks from an obviously green C programmer!
 
J

Jack Klein

Is it possible to write a function that uses stdout for both output
and input? What I mean to say is, I can obviously write a line to
stdout to ask for input, ie:

fprintf(stdout, "%s\n", prompt);

Then in order to read the response, I would use:

fgets(user_input, MAX_LINES, stdin);

Is that the correct way to do it? Is there ever an instance where
this would work:

fgets(user_input, MAX_LINES, stdout);

Thanks from an obviously green C programmer!

You can't use stdout for input. It is opened in output mode only.
 
J

Jeremy Yallop

Is it possible to write a function that uses stdout for both output
and input?

Perhaps, but there's no reason to do so other than obfuscation. The
standard function freopen() can be used to associate stdout with a
particular file. If there is a special file associated with the
terminal on your platform (such as /dev/tty on Unix) then you can
attempt to associate stdout with that file in read-write mode. For
example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char buf[128];
if (freopen("/dev/tty", "r+", stdout)
&& fputs("prompt> ", stdout) != EOF
&& fgets(buf, sizeof buf, stdout) != NULL
&& fputs(buf, stdout) != EOF)
return EXIT_SUCCESS;
else return EXIT_FAILURE;
}

Jeremy.
 
R

Richard Bos

Jeremy Yallop said:
Perhaps, but there's no reason to do so other than obfuscation. The
standard function freopen() can be used to associate stdout with a
particular file.

It's not guaranteed to succeed, though.

Richard
 
V

Villy Kruse

Is it possible to write a function that uses stdout for both output
and input?

Perhaps, but there's no reason to do so other than obfuscation. The
standard function freopen() can be used to associate stdout with a
particular file. If there is a special file associated with the
terminal on your platform (such as /dev/tty on Unix) then you can
attempt to associate stdout with that file in read-write mode. For
example:

#include <stdio.h>
#include <stdlib.h>

int main()
{
char buf[128];
if (freopen("/dev/tty", "r+", stdout)
&& fputs("prompt> ", stdout) != EOF
&& fgets(buf, sizeof buf, stdout) != NULL
&& fputs(buf, stdout) != EOF)
return EXIT_SUCCESS;
else return EXIT_FAILURE;
}

Jeremy.

I beleive you need a fseek() between fputs and fgets. You may find that
fseek() on an interactive device may not work, though.


Villy
 
R

Ross Kendall Axe

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

Villy Kruse wrote:
| On 3 May 2004 08:10:03 GMT,
|
|
|
|>[email protected] wrote:
|>
|>>Is it possible to write a function that uses stdout for both output
|>>and input?
|>
|>Perhaps, but there's no reason to do so other than obfuscation. The
|>standard function freopen() can be used to associate stdout with a
|>particular file. If there is a special file associated with the
|>terminal on your platform (such as /dev/tty on Unix) then you can
|>attempt to associate stdout with that file in read-write mode. For
|>example:
|>
|> #include <stdio.h>
|> #include <stdlib.h>
|>
|> int main()
|> {
|> char buf[128];
|> if (freopen("/dev/tty", "r+", stdout)
|> && fputs("prompt> ", stdout) != EOF
|> && fgets(buf, sizeof buf, stdout) != NULL
|> && fputs(buf, stdout) != EOF)
|> return EXIT_SUCCESS;
|> else return EXIT_FAILURE;
|> }
|>
|>Jeremy.
|
|
| I beleive you need a fseek() between fputs and fgets. You may find that
| fseek() on an interactive device may not work, though.
|
|
| Villy

I believe it's a fseek() or a fflush(). fflush() would seem to make more
sense, not that this thread is really making too much sense anyway, so
maybe I shouldn't worry too much about it ;-)

Ross
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFAl7Dr9bR4xmappRARAmEsAKCTXvGhC+czvSnhh75nQbHaqoE0AQCgpFJp
FVIIfHyHeWVEkh+8deBvxoY=
=3eKz
-----END PGP SIGNATURE-----
 

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

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top