Why is it recommended to have empty line in the en

2019-01-12 21:49发布

Some code style tools recommend this and I remember seeing some unix command line tools warning about missing empty line.

What is the reasoning for having an extra empty line?

8条回答
Summer. ? 凉城
2楼-- · 2019-01-12 22:24

An argument can also be made for cleaner diffs if you append to the file following the same reasoning as Why does Python allow a trailing comma in list?

查看更多
再贱就再见
3楼-- · 2019-01-12 22:28

If you try to concatenate two text files together, you will be much happier if the first one ends with a newline character.

查看更多
【Aperson】
4楼-- · 2019-01-12 22:29

The empty line in the end of file appears so that standard reading from the input stream will know when to terminate the read, usually returns EOF to indicate that you have reached the end. The majority of languages can handle the EOF marker. It is there for that reason from the old days, under DOS, the EOF marker was F6 key or Ctrl-Z, for *nix systems, it was Ctrl-D.

Most, if not all, will actually read right up to the EOF marker so that the runtime library's function of reading from input will know when to stop reading any further. When you open the stream for Append mode, it will wipe the EOF marker and write past it, until a close is explicitly called in which it will insert the EOF marker at that point.

Older tools were expecting a empty line followed by EOF marker. Nowadays, tools can handle the empty line and ignore it.

查看更多
唯我独甜
5楼-- · 2019-01-12 22:38

Many older tools misbehave if the last line of data in a text file is not terminated with a newline or carriage return / new line combination. They ignore that line as it is terminated with ^Z (eof) instead.

查看更多
冷血范
6楼-- · 2019-01-12 22:42

Some languages define their input file in terms of input lines, where each input line is a series of characters terminated by a carriage return. If their grammar is so defined, then the last valid line of the file must be terminated by a carriage return too.

查看更多
We Are One
7楼-- · 2019-01-12 22:47

Also when you modify file and appends some code at the end of file - diff (at least git diff in standard coniguration) will show that you changed the last line, while the only thing you've actually done - added a newline symbol. So cvs reports become less convenient.

查看更多
登录 后发表回答