Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 936
kim taeyun Avatar asked Dec 13 '22 14:12

kim taeyun


2 Answers

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.

like image 144
Keith Avatar answered Dec 15 '22 02:12

Keith


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.

like image 23
Tony Delroy Avatar answered Dec 15 '22 04:12

Tony Delroy