I have a program that I start from a command line with a file pushed to stdin
:
main.exe < file.bin
I read all the contents from the file like so:
freopen(NULL, "rb", stdin); // it's a binary file
long* buffer = new long[16000];
while ( fread(buffer, sizeof(long), 16000, stdin) == 16000);
After all numbers are read, I would like the user to confirm continuation in the program by pressing any key but at that point stdin
probably contains EOF
and the program skips the confirmation from the user.
printf("Press any key to continue");
getchar(); // user doesn't get a chance to press anything, program flows right through
Here's the whole program if you want to reproduce it. But to reproduce you have to push a file to stdin before execution.
#include <stdio.h>
int main()
{
freopen(NULL, "rb", stdin);
long* buffer = new long[16000];
while ( fread(buffer, sizeof(long), 16000, stdin) == 16000);
printf("Press any key to continue");
getchar();
delete[] buffer;
return 0;
}
The question is how to make the getchar()
call take an character from the user's keyboard and not the file that was already read.
The program runs as C++17 but uses a lot of C style code. If you know a way how to do this with C++ streams feel free to post that as well. I use Windows 10 but I would also like to see some portable code.