views:

371

answers:

2

When I'm generating a text file programatically, should I insert the ASCII EOF marker (decimal value 26) at the end of the file?

Do the .NET Programming Languages do this automatically?

+5  A: 

No. The EOF is produced by the OS's IO functions when reading the file. It should not be stored in the file in modern systems.

Matthew Flaschen
+2  A: 

There's no reason for the ^Z EOF marker anymore (and there hasn't been for a long time) - it's a hold over from CP/M which did not support exact lengths for file sizes in the directory - file sizes were in terms of the number of 128 byte blocks, so to end a file on a non-128 byte boundary you had to use an EOF character.

Since early versions of MS-DOS were heavily influenced by CP/M (and Microsoft wanted CP/M programs to port easily), the convention stuck.

Your program should open text files with the appropriate attributes so the OS and/or language runtime will signal an EOF when it sees a ^Z in case you come across a file that uses the convention. But there's no need to write one anymore.

One possible exception is if you have a binary file, but want to put some text at the start of it, then a ^Z, then your data. If someone dumps it to the console it'll say something intelligent instead of spewing garbage. There's not a whole lot of reason to do this, but I've seen that done rarely.

From Wikipedia (http://en.wikipedia.org/wiki/CP/M):

File size was specified as the number of 128-byte records (directly corresponding to disk sectors on 8-inch drives) occupied by a file on the disk. There was no generally supported way of specifying byte-exact file sizes. The current size of a file was maintained in the file's file control block (FCB) by the operating system. Since many application programs (such as text editors) prefer to deal with files as sequences of characters rather than as sequences of records, by convention text files were terminated with a control-Z character (ASCII SUB, hexadecimal 1A). Determining the end of a text file therefore involved examining the last record of the file to locate the terminating control-Z. This also meant that inserting a control-Z character into the middle of a file usually had the effect of truncating the text contents of the file.

Michael Burr