Why should text files end with a newline?

后端 未结 18 1127
栀梦
栀梦 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.

    0 讨论(0)
  • 2020-11-21 23:20

    Imagine that the file is being processed while the file is still being generated by another process.

    It might have to do with that? A flag that indicates that the file is ready to be processed.

    0 讨论(0)
  • 2020-11-21 23:21

    There's also a practical programming issue with files lacking newlines at the end: The read Bash built-in (I don't know about other read implementations) doesn't work as expected:

    printf $'foo\nbar' | while read line
    do
        echo $line
    done
    

    This prints only foo! The reason is that when read encounters the last line, it writes the contents to $line but returns exit code 1 because it reached EOF. This breaks the while loop, so we never reach the echo $line part. If you want to handle this situation, you have to do the following:

    while read line || [ -n "${line-}" ]
    do
        echo $line
    done < <(printf $'foo\nbar')
    

    That is, do the echo if the read failed because of a non-empty line at end of file. Naturally, in this case there will be one extra newline in the output which was not in the input.

    0 讨论(0)
  • 2020-11-21 23:25

    I've wondered this myself for years. But i came across a good reason today.

    Imagine a file with a record on every line (ex: a CSV file). And that the computer was writing records at the end of the file. But it suddenly crashed. Gee was the last line complete? (not a nice situation)

    But if we always terminate the last line, then we would know (simply check if last line is terminated). Otherwise we would probably have to discard the last line every time, just to be safe.

    0 讨论(0)
  • 2020-11-21 23:26

    I personally like new lines at the end of source code files.

    It may have its origin with Linux or all UNIX systems for that matter. I remember there compilation errors (gcc if I'm not mistaken) because source code files did not end with an empty new line. Why was it made this way one is left to wonder.

    0 讨论(0)
  • 2020-11-21 23:27

    Basically there are many programs which will not process files correctly if they don't get the final EOL EOF.

    GCC warns you about this because it's expected as part of the C standard. (section 5.1.1.2 apparently)

    "No newline at end of file" compiler warning

    0 讨论(0)
提交回复
热议问题