The requirement of my program is, it should move to next line without waiting for the user to enter while using Scanf(). The user have to enter the input within that period of time. What I can do so that my program execute continuously?
Asked
Active
Viewed 142 times
-1
-
you can try it in here: https://stackoverflow.com/questions/28130703/control-scanf-duration-for-taking-any-input – nocolor06 Jun 28 '22 at 02:52
-
1Does this answer your question? [How can I prevent scanf() to wait forever for an input character?](https://stackoverflow.com/questions/21197977/how-can-i-prevent-scanf-to-wait-forever-for-an-input-character) – 273K Jun 28 '22 at 02:54
-
This is not possible in plain ISO C. You will therefore require platform-specific functionality in order to accomplish this task. For this reason, please specify to which platform (e.g. operating system) your question applies, by tagging your question accordingly. – Andreas Wenzel Jun 28 '22 at 03:21
1 Answers
-1
I do not recall any platform independent method to get what you are looking for with scanf(), But there are platform specific ways.
If you are using Windows you can make use of the <windows.h>
console api for getting non blocking input.
Define a function to get a single character input from user which immediately quits after the user presses any key.
#include <windows.h>
TCHAR ourgetch(HANDLE ipHandle) {
DWORD con_mode, byteswritten;
GetConsoleMode(ipHandle, &con_mode);
SetConsoleMode(ipHandle, con_mode & ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT));
TCHAR ch = 0;
ReadConsole(ipHandle,
&ch, 1,
&byteswritten, NULL);
SetConsoleMode(ipHandle, con_mode);
return ch;
}
And now you can use that in your main to call it in a loop to get an input string.
char* getInput () {
char buff[1000];
int i = 0;
while (! some_global_timer_flag) {
key = ourgetch(ipHandle);
buff[i++] = key;
}
return buff;
}
int main () {
HANDLE ipHandle = GetStdHandle(STD_INPUT_HANDLE);
char* buffer = getInput();
// use buff
}
You can then use your preferred multithreading API to run getInput()
in a thread while having a Timer()
thread with a global flag that will interrupt and end the input when time is up, getInput()
is taking input.

marc_s
- 732,580
- 175
- 1,330
- 1,459

TheMedicineSeller
- 36
- 1
- 4
-
When using a blocking input function, reading one character at a time does not make the function "non-blocking". OP wants the program to do something else after a timeout has expired, even if not a single key was pressed. Therefore, you will need a proper non-blocking function. See [this answer of mine](https://stackoverflow.com/a/72394274/12149471) to another question for such an example. – Andreas Wenzel Jun 28 '22 at 05:45
-
Yes It was my bad that I Did not specify a method to actually interrupt the function mid-input. The idea is to run the input getter function on a thread and have a timer thread that will interrupt the input getter thread upon time up. I wasn't familiar with the multi-threading API in windows that is why I ommited it out. – TheMedicineSeller Jun 29 '22 at 09:01
-
I'm afraid that your approach will not work on Microsoft Windows. Setting a global flag will not interrupt a blocking call to `ReadConsole`. Also, creating a separate thread would also be useless, because, as far as I can tell, there is no function in the Windows API that is able to interrupt a blocking call to `ReadConsole`. The functions [CancelSynchronousIo](https://learn.microsoft.com/en-us/windows/win32/fileio/cancelsynchronousio-func) and [CancelIoEx](https://learn.microsoft.com/en-us/windows/win32/fileio/cancelioex-func) do not seem to support Console I/O. – Andreas Wenzel Jun 29 '22 at 17:12