Jin
Jin

Reputation: 1948

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?

Upvotes: 7

Views: 501

Answers (1)

Hans Passant
Hans Passant

Reputation: 942267

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.

Upvotes: 4

Related Questions