简体   繁体   中英

Does there exist an “unput” for std::ostream like there exists “unget” for std::istream?

I have some parsing code that allows for escape sequences to be entered into a string of text:

// In a file or large, multi-line string ...
my_parameter="A setting for the parameter\nthat contains \"escape sequence\" characters"

When I parse it, I handle the backslashes and add the appropriate character to the string that I am building using a std::ostringstream instance. Line feeds, quotes, backslashes and such all work fine. However, I was contemplating whether or not to allow the \\b sequence, and went looking to see if I can "unput" the last character from my ostringstream like you can "unget" from any std::istream . Can you do such a thing? If the function does not exist, is there a simple way to push the write position back one character and simply have the next character read overwrite it?

This is not mission-critical or anything like that, but I was curious if anyone else has come across this before.

Streams are an awful lot like the mail. each message sent on a stream is like a letter, and messages can be queued into buffers, which are like mailboxes.

If you were responsible for both putting messages into and taking messages out of a mail-box, then you could certainly know that a letter you just put there is still there for you to take back. Of course, you probably wouldn't go to the trouble of putting it in a mailbox at all, since you own both ends.

If instead, you are putting a letter in your girlfriends mailbox, you don't really have much control of when she will check her mailbox and take out all of the letters. it could be that she's sitting by the door and will snatch the letter right up and read it as soon as it passes through the slot.

More likely, you're actually delivering the letter to a box owned by the post-office (the operating system). Although many such receptacles are just bins, and mail carrier checks it once per day, It could be that the slot is connected directly to a sorting machine, and the letter gets delivered the instant you drop it.

In a streaming interface with concurrency, there is no general way to retake ownership of a write once written. If you need that, you should place an intermediate buffer between you and the stream, and flush it out to the stream only when you know for sure that you are ready.

您可以使用seekp在流中设置光标的位置(请参阅: httpseekp )。

If you may want to take back a character, don't send it until you're sure you won't want to take it back. You could implement your own "allow takebacks" logic thus:

int pending_ch = -1;
void output_char(int ch)
{
  if (pending_ch >= 0)
    putch(F, ch);
  pending_ch = ch;    
}
void unput_char(void)
{
  pending_ch = -1;
}
void force_put_char(void)
{
  output_char(-1);
}

A bit clunky, but that general approach can be useful for delaying output to a stream.

You could simply input a backspace character yourself. I've done it in the Windows Console, at least. This should ensure that the correct behaviour is observed regardless of the destination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM