Original Post
In my C++ console application, I need to be able to fake input by programmatically inserting data into
cin. The purpose of this is to create an alias system. For example, if the user enters "gravity," I want the program to extract that and then insert "-9.8" into the stream, as if that''s what the user really entered. This all goes on with unformatted input functions. What I do at the moment is call cin.peek to fill the stream buffer, use cin.rdbuf()->in_avail() to allocate a buffer of the right size, and then call cin.getline to retrieve the string. What I do after that I have not been able to figure out, and that''s my question to you all. Once all that is accomplished however, the alias system is finished and the next extraction should only see the "-9.8" in the stream buffer. Mission Accomplished.
The only way that even remotely comes close to working is by using cin.putback to "back through" the string and insert its characters into the input stream. However, the major problem is that I can only put back at most as many characters as were originally extracted with getline, so unless I am putting back the exact amount of characters are were originally extracted, it doesn''t work.
I also tryed using sputc and sputn, however they had no effect on the buffer. It didn''t change. I then tried something kinky like this:
ostream outStream( cin.rdbuf() ); outStream << myAlias;In a desperate attempt to use the input stream as an output stream... but still no luck. I have a feeling I''m on the right track, but missing something trivial like an intermediary flush.