I'm trying to read data from stdin, in C++ on Windows, efficiently, which means preferably in large chunks. This can be done with:
ReadFile(GetStdHandle(STD_INPUT_HANDLE), buf, bytestoread, &bytesread, 0);
or
read(0, buf, bytestoread);
But in both cases, it only works if bytestoread
is set to a very small number e.g. 50; if set to a larger number e.g. one megabyte, the call fails with 'not enough space' error, as though the data were not going directly to the buffer I provide, but instead being copied via some internal buffer of fixed size. This is true whether the input is piped, or typed on the console.
Is Windows just limited in how large a chunk a process can read from stdin at a time? If so, what's the maximum chunk size that is guaranteed to work?
A complete program that shows the problem:
#include <errno.h>
#include <io.h>
#include <stdio.h>
#include <string.h>
char buf[1000000];
int main(int argc, char **argv) {
auto r = read(0, buf, sizeof buf);
if (r < 0)
perror("read");
return 0;
}