开发者

in linux, is it normal that there is no null character at the end of file

in linux, is it normal that there is no null character 开发者_高级运维at the end of file?

I made a empty file and open with mouse pad write az.

save it.

when I open the file up with hex editor, there is no null character but 0a is there.

what null character should I put the end of file?

when I write the file with system call.

is it 0a? or 0?

thanks


There is usually no null character at the end of files on Unix. An empty text file has zero bytes. One empty line will have a 0x0A (LF, linefeed) character. Unix text files have single LF line endings.


The filesystem records the number of bytes in a file, and all the bytes are free to have any value - no particular character/byte value is a reserved sentinel value meaning end-of-file. So, you can have a NUL anywhere in the file, but don't need one to mark the end.

Each line in a text file should indeed be terminated with a linefeed, ASCII 10 dec, 0A hex (on Windows it'd be a carriage return ASCII 13 dec followed by a linefeed). If you create an empty file ala echo > filename it will have one linefeed, but only because echo prints an empty line by default. If you instead used touch filename it would be completely empty.

When you cat > filename and type things into your terminal/console window, you eventually use Control-D to trigger an end-of-file condition (for Linux / Control-Z in DOS), but that character is not stored in the file itself.


0a is the newline, aka control-J or \n. Text files don't normally end with a null character in Unix.


It depends on who implemented the file format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜