Read from file or stdin

前端 未结 6 1193
北恋
北恋 2020-11-30 01:08

I am writing a utility which accepts either a filename, or reads from stdin.

I would like to know the most robust / fastest way of checking to see if stdin exists (d

6条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 01:53

    You're thinking it wrong.

    What you are trying to do:

    If stdin exists use it, else check whether the user supplied a filename.

    What you should be doing instead:

    If the user supplies a filename, then use the filename. Else use stdin.

    You cannot know the total length of an incoming stream unless you read it all and keep it buffered. You just cannot seek backwards into pipes. This is a limitation of how pipes work. Pipes are not suitable for all tasks and sometimes intermediate files are required.

提交回复
热议问题