Proper/Efficient way to determine size of stdin in C

后端 未结 5 829
逝去的感伤
逝去的感伤 2021-01-16 21:49

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

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-16 22:16

    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.

提交回复
热议问题