In an attempt to create a simple cat clone in python,
sys.stdout.write(sys.stdin.read()) I noticed that this fails horribly for binary files (i.e. python cat.py < binaryfile > supposed_copy) containing the CTRL+Z EOF / substitude character 0x1a, since that seems to cause read() to consider its work done. I cannot simply loop over the code forever to circumvent this, since obviously at some point stdin.read() will wait until new input is provided, which once the true end of input is reached won't ever happen.
So, how can this be fixed, i.e.
- How to know when the file redirected to
stdinis fully read, or - how to properly handle this?
openstatement please?_setmode, which you can call on the associated C file handle (and, as an implementation detail, the C++ streams on Windows will use that). However, that’s then of course Windows specific, and relies on non-standardised behaviour of the streams. In practice this means it can be achieved, but requires platform- and compiler specific code.