3

I'm searching for a function that will print a string before the input is made. I know this is possible with printf and gets, but I want the string to stay on the last line where the input is given. The output is handled in the main thread, the input is handled in another thread started by the main thread.

For example this is the current console window (I have input the stop-command, the '>' is the prefix string):

[12:00:00] Starting server...
>stop

When the console outputs a new line, I want the input to be always on the bottom line, like this:

[12:00:00] Starting server...
[12:00:01] Server started
>stop

I am using Embarcadero C++Builder XE2 with Win32 and VCL support.

EDIT: I'm currently using this code, resulting in printing the output after the '>' prefix:

char buf[256];
printf(">");
gets(buf);
2
  • You can use win32 api to set the console cursor and write to that location. I'm a little confused as to what you want, but you can print at the cursor and move it around. It's just that the cursor is usually at the bottom of the console while things are printing. msdn.microsoft.com/en-us/library/windows/desktop/… Commented May 9, 2013 at 16:05
  • Don't use gets ever for any reason. Use fgets instead. Commented May 9, 2013 at 16:18

2 Answers 2

4

What you are looking for is called asynchronous input/output.

What I did to accomplish this is keeping track of each key being pressed and append it to a string.

When you press backspace it deletes the last character added to the string and when you press enter it will submit the current command.

When output comes along you clear the line you were typing on, print the output, and then move the cursor down and print out what you had saved in the string.

You have to do a little work with threading if you want but I'm pretty sure I was able to accomplish most of it without too much effort.

Other possible resources:

Sign up to request clarification or add additional context in comments.

2 Comments

This way, while perfectly eligible, will require implementation of all the edit functions (arrow movements, clipboard operations etc.), otherwise some users will be confused.
@Inspired You're right! I had to write that functionality in myself. In a pure console environment you might not allow copy/paste but the history keys are very useful.
0

Try scrolling all lines of the console except for the last line and then printing your message into the line before the last (something like ScrollConsoleScreenBuffer will help you). This way the input line will always be at its place.

1 Comment

I understand your answer, but can you provide me an example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.