Why should text files end with a newline?

后端 未结 18 1733
栀梦
栀梦 2020-11-21 22:56

I assume everyone here is familiar with the adage that all text files should end with a newline. I\'ve known of this \"rule\" for years but I\'ve always wondered — why?

18条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 23:18

    This originates from the very early days when simple terminals were used. The newline char was used to trigger a 'flush' of the transferred data.

    Today, the newline char isn't required anymore. Sure, many apps still have problems if the newline isn't there, but I'd consider that a bug in those apps.

    If however you have a text file format where you require the newline, you get simple data verification very cheap: if the file ends with a line that has no newline at the end, you know the file is broken. With only one extra byte for each line, you can detect broken files with high accuracy and almost no CPU time.

提交回复
热议问题