0

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.

sanitizedUser
  • 1,723
  • 3
  • 18
  • 33
  • 2
    Sounds to me like you're trying to hammer a square peg into a round hole. Why not just provide the input filename as a command line argument (`main.exe file.bin`) and keep `stdin` free for user input when you need it? – r3mainer Jun 18 '20 at 14:13
  • Open the file with `fopen("File.bin", "rb")` Also the code will ignore the early part if it has more than 16000 elements. Better to capture the return value from `fread`. – Weather Vane Jun 18 '20 at 14:13
  • If it's running as C++17, `` is worth learning. – sweenish Jun 18 '20 at 14:14
  • I know that would work, but in my environment it's currently not possible. I can only do `<` operation. – sanitizedUser Jun 18 '20 at 14:14
  • @sanitizedUser what makes impossible to use `fopen` or `` in your environment ? – Nicolas Dusart Jun 18 '20 at 14:18
  • @NicolasDusart I send a solution code to a server that uses the `<` operation and I cannot change that. Anyway, this was just a test for me to test reading of the file. On the server there is no user that can press keyboard obviously. – sanitizedUser Jun 18 '20 at 14:40
  • What's the point of the final `getchar()` anyway. Is it for avoiding Windows to close the terminal window ? It is already started from terminal to pass file as stdin, just let it close the program, the terminal will be kept open. – Nicolas Dusart Jun 18 '20 at 15:07
  • @NicolasDusart The test version is running under VS2019 and I don't want to set breakpoint at the last line because sometimes I carry the project around between different IDEs. – sanitizedUser Jun 18 '20 at 16:32
  • @Adler If I wanted to provide more detail I would have answered instead of commented. But, I mean, `` is the C++17 way of working with files. – sweenish Jun 18 '20 at 18:07
  • @sweenish, how is that supposed to be helpful, given the question at hand? – Hrisip Jun 18 '20 at 18:11

1 Answers1

1

If you use a file file.bin as a stdin via main.exe < file.bin, you cannot simultaneously use console as a stdin using standard C or C++ libraries. Maybe you could try it with WinAPI https://learn.microsoft.com/en-us/windows/console/readconsoleinput And using std::cin to read binary data is unfortulately not possible: Read binary data from std::cin

Lime
  • 28
  • 5