Can a process close and reopen stdin 
Author Message
 Can a process close and reopen stdin

Hi,

Can I close and reopen stdin from a C program. Something like..

fclose(stdin);
idontknowwhat_open(stdin);

The problem that I have:

I print a menu and a prompt and get a string from the user. The string
can have spaces, so I use fgets on stdin. Before I do fgets, I do
fflush(stdin/out).

This works fine in most of the cases except when I do a system ioctl,
after which it doesnt wait on the prompt!

After the ioctl, fgets is returning NULL. When I check the status of
stdin (using) ferror, it says that there is an error on stdin (ferror
returns true, feof returns false). There is a function
clearerr(stream) that allows one to clear the errors from a stream. I
tried that on stdin, but that also doesn't work.

The only thing I can think of now is to reopen stdin in some way, if
possible, and try..

If you know how to do this or know of another solution to this
problem, please inform. I tried reading characters, one at a time, in
a loop and that also does not work.

The OS is BSD/OS 4.3 The same code works fine on Linux (fgets never
returns NULL). BSD/OS also has a call "fpurge" that purges data from a
stream. I tried that one too, doesn't work.

Thanks
Ash



Tue, 22 Mar 2005 06:27:09 GMT  
 Can a process close and reopen stdin


Quote:
>Hi,

>Can I close and reopen stdin from a C program. Something like..

>fclose(stdin);
>idontknowwhat_open(stdin);

>The problem that I have:

>I print a menu and a prompt and get a string from the user. The string
>can have spaces, so I use fgets on stdin. Before I do fgets, I do
>fflush(stdin/out).

fflush(stdin) is undefined behavior. So anything you don't expect may
occure or not.
You can't flush stdin. read anything that is there until '\n' occures
will help.

--
Tschau/Bye
        Herbert

http://www.pc-rosenau.de
the point to buy eComStation in germany



Tue, 22 Mar 2005 08:21:11 GMT  
 Can a process close and reopen stdin

Quote:

> fflush(stdin) is undefined behavior. So anything you don't expect may
> occure or not.
> You can't flush stdin. read anything that is there until '\n' occures
> will help.

Do not forget to check for EOF as well.

--
Thomas.



Tue, 22 Mar 2005 17:19:53 GMT  
 Can a process close and reopen stdin

Quote:


> > fflush(stdin) is undefined behavior. So anything you don't expect may
> > occure or not.
> > You can't flush stdin. read anything that is there until '\n' occures
> > will help.

> Do not forget to check for EOF as well.

The thing is that there is some error on stdin. ferror returns > 0.
EOF hasn't been reached yet (feof = 0).

I am reading from the stream and this thing continuously happens, no
matter how many reads I do. E.g. I put this thing in a loop and read
using scanf, I always get the same input (whatever was given as the
last command at the prompt).

Another thing is that after doing fgets, perror prints "Bad file
descriptor"..

This doesn't happen all the time on that OS. It is only when I do this
particular ioctl that this happens.. Could this be a problem in the
OS?

Thanks.



Wed, 23 Mar 2005 00:28:10 GMT  
 Can a process close and reopen stdin

Quote:
> Hi,

> Can I close and reopen stdin from a C program. Something like..

> fclose(stdin);
> idontknowwhat_open(stdin);

> The problem that I have:

> I print a menu and a prompt and get a string from the user. The string
> can have spaces, so I use fgets on stdin. Before I do fgets, I do
> fflush(stdin/out).

Well, it certainly is failure to fflush(stdin) because fflush is
only defined for output streams or bidirectional where the last
operation was output. So you may probably want to change this to

{
  int c;
  while((c=fgetc(stdin)) != '\n' && c != EOF);

Quote:
}

That is data residing in the input buffer up to the next newline
character will be read, or if there was no newline in the buffer
the system will be asked to provide more data.

Quote:

> This works fine in most of the cases except when I do a system ioctl,
> after which it doesnt wait on the prompt!

> After the ioctl, fgets is returning NULL. When I check the status of
> stdin (using) ferror, it says that there is an error on stdin (ferror
> returns true, feof returns false). There is a function
> clearerr(stream) that allows one to clear the errors from a stream. I
> tried that on stdin, but that also doesn't work.

Apperently this is to be discussed in a system or platform
related newsgroup, because this behaviour appears due to some
system specific code in your program. The C language doesn't
define ioctl() or the behavior of a program calling it.

Quote:

> The only thing I can think of now is to reopen stdin in some way, if
> possible, and try..

freopen() springs to my mind. But I don't know whether that
changes anything.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Fri, 25 Mar 2005 16:51:05 GMT  
 Can a process close and reopen stdin

Quote:

>> Do not forget to check for EOF as well.


Quote:

>The thing is that there is some error on stdin. ferror returns > 0.
>EOF hasn't been reached yet (feof = 0).

A decent ("good quality-of-implementation") system will set errno
in this case, although the C standard does not require this.

Quote:
>Another thing is that after doing fgets, perror prints "Bad file
>descriptor"..

As it happens (off-topic for comp.lang.c), this means "the underlying
file in the O/S has been revoked in some way" (or was never opened
for reading, but obviously it was open that way earlier).

Quote:
>This doesn't happen all the time on that OS. It is only when I do this
>particular ioctl that this happens.. Could this be a problem in the
>OS?

It could be; or the ioctl -- which is unfortunately also off-topic
for comp.lang.c -- is one that means: "please take away all my access
to the file descriptor". :-)

(OT redirect: you might try the bsdi-users mailing list, which is

archives of the list as well; no doubt a Google search will find
them.)
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)

http://67.40.109.61/torek/  (for the moment)
(you probably cannot email me -- spam has effectively killed email)



Thu, 31 Mar 2005 13:32:16 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Closing/reopening stdin in binary mode w/ Turbo C 2.0

2. reopening stdin and stdout

3. passing stdin to a forked process, after writing some info to the process

4. DAO multiple Addnew's requires closing and reopening of Recordset

5. How to close and reopen an OLE DB?

6. How to close and reopen OLE DB?

7. How to reopen closed file descriptors ????

8. Reopening closed project windows in Borland C++ 4.0

9. How to close and reopen an OLE DB?

10. can't reopen the Toolbar after it closed

11. State of buffering after a close and then a reopen

12. Created Process dies when parent process closed

 

 
Powered by phpBB® Forum Software