Beefy Boxes and Bandwidth Generously Provided by pair Networks
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
in thread non-destructive read on stdin by jeremyh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-20 03:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found