Based on my previous question, what would be the proper yet efficient way to determine the size of stdin
if stdin is coming from a pipe
or a
Contrary to what has been said, you can use stat()
to determine the amount of data in a pipe or stream in some OS. This will not, however, work on all systems -- that is, it's not standard practice for the implementation to provide this functionality, so doing it this way will NOT be portable.
There's also the issue of a pipe size changing whenever someone writes more data to it, which can happen basically at any time in a multitasking, preemptive system.
So, while you can tiptoe your way around what you want to do, it's a weak and non-portable solution.
Of course, reading every byte until EOF or error, and counting, will still work. Not sure that's what you want, though.