Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the operating system recognize end of a text file?

For example, there is a text file called "Hello.txt"

Hello World!

Then how does the operating system (I'm using MS-DOS) recognize the end of this text file? Is some kind of character or symbol hidden after '!' which indicates the end of file?

like image 211
Jin Avatar asked Aug 26 '16 06:08

Jin


People also ask

How does Linux know End of file?

Modern operating systems, such as Linux, do not use the file contents to detect the end of file, they store the file length in the file system and keep track of the file position to determine if more contents is available to read.

What character is at the end of a file?

EOF character By default, the driver converts a Control-D character at the start of a line into an end-of-file indicator. To insert an actual Control-D (ASCII 04) character into the input stream, the user precedes it with a "quote" command character (usually Control-V).

What is the end of file character in Linux?

The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.

How do I add an EOF to a text file?

You can use the :read command to insert the contents into the file. That command takes a filename, but you can use the /dev/stdin pseudo-device to read from standard input, which allows you to use a <<EOF marker.


1 Answers

If you use MS-Dos then there are some odds that there is indeed a special character at the end of the string. MS-Dos was derived from Tim Paterson's QDos who wrote it to be as compatible as possible with the then-dominant CP/M. An OS for 8-bit machines, it kept track of a file size by only counting the number of disk sectors used by the file. Which made the file size always a multiple of 128 bytes.

Which required a hack to indicate the real end of a text file, since it could be located in the middle of a sector, it used the Ctrl+Z control character (character code 0x1A). Which required a language runtime implementation to remove it again and declare end-of-file when it encounters the character. Ctrl+Z is not quite forgotten, it still works when you type it in a Windows console to terminate input. Compare to Ctrl+D in a Unix terminal.

Whether it actually is present in the file depends on what program created the file. Which would have to be an MS-Dos program as well to get the Ctrl+Z appended. It is certainly not required. Paterson improved on CP/M to remove some of its restrictions, greatly aided by having a lot more address space available (1 MB vs 64 KB), MS-Dos keeps track of the actual number of bytes in a file. So it can always reliable indicate the true end of a file. Which is probably the most accurate answer to your question.

Ancient history btw, invest your time wisely.

like image 173
Hans Passant Avatar answered Sep 30 '22 21:09

Hans Passant