How to Write an Eof Character Ourselves

Can we write an EOF character ourselves?

There is no EOF character. EOF by definition "is unequal to any valid character code". Often it is -1. It is not written into the file at any point.

There is a historical EOF character value (CTRL+Z) in DOS, but it is obsolete these days.

To answer the follow-up question of Apoorv: The OS never uses the file data to determine file length (files are not 'null terminated' in any way). So you cannot trick the OS. Perhaps old, stupid programs won't read after CTRL+Z character. I wouldn't assume that any Windows application (even Notepad) would do that. My guess is that it would be easier to trick them with a null (\0) character.

C# text file - how to write EOF character

Just use the StreamWriter class, and Close the file appropriately. The EOF marker will be handled properly for you.

Representing EOF in C code?

EOF is not a character (in most modern operating systems). It is simply a condition that applies to a file stream when the end of the stream is reached. The confusion arises because a user may signal EOF for console input by typing a special character (e.g Control-D in Unix, Linux, et al), but this character is not seen by the running program, it is caught by the operating system which in turn signals EOF to the process.

Note: in some very old operating systems EOF was a character, e.g. Control-Z in CP/M, but this was a crude hack to avoid the overhead of maintaining actual file lengths in file system directories.

File contains two EOF characters; what happens?

In Unix there is no EOF character. It's simply a concept, a value returned by getc to signal "this is the end (beautiful friend)". EOF is chosen so that getc (and friends) can't return it in any other case.

And about writing past the end of file, different filesystems do things differently.

  • Some will leave holes that don't actually occupy any space on the disk
  • Some will fill in the blanks with blanks (0)

Is there an already existing EOF character in python?

read of files returns an empty string when EOF is encountered.

while True:
chunk = fp.read(1)
if chunk == '':
break


Related Topics



Leave a reply



Submit