Keep It Simple, Stupid | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
I think what you're looking for is an unget_char() routine to work with a getchar() routine. (Actually unget_str(), but it's easier to explain at the character level. And it has historical precedence in C, although it's neithe ANSI nor Posix standard.)
To make this work rolling your own, you need to pull all input from a single routine, a getchar() routine, and that routine needs to have an extra buffer that you can put characters back into, when you don't want them yet. getchar() takes bytes from that extra buffer, in order, and only when the extra buffer's empty does it go to STDIN itself. This is the only way to maintain the sequence of characters comming in. If you try to load them back in by stuffing them into STDIN, then you will always have a race condition where a new character typed in can get stuffed in after you took characters out, but before you put them back in. This will happen rarely and unpredictably, and possibly more often when the system is loaded. It will probably not show up in testing. Update: Looks like I misunderstood what the OP wanted. I should have read "process" as "OS process", which makes sense to do, but I missed it. Oh well, it was a decent answer to a different question, I think. (And thanks to [id://betterworld] for pointing out the error.) In reply to Re: non-destructive read on stdin
by rodion
|
|