Write End Of File (EOF)

32 views (last 30 days)
Julio
Julio on 23 May 2013
Hi, everybody, I want to make a program to read a file (this file has already information), and set the End of File in any part of the file, is it possible?

Answers (3)

Walter Roberson
Walter Roberson on 23 May 2013
MATLAB does not have any way to set a true End of File if it is smaller than the original file (but files can be lengthened.)
You could fwrite() a control-Z, also known as EOF (End of File), if that is meaningful for you (which would require MS Windows and reading the file in "text" mode)

Iain
Iain on 23 May 2013
If by EOF you mean the ascii code "4", all you need to do is fwrite(fid,4,'uint8'); on the open file.
You can do the same with any other end of file marker provided you know its binary value.
  2 Comments
Jan
Jan on 23 May 2013
As Walter has pointed out already: This works only under Windows and when the file is opened in text mode.
Walter Roberson
Walter Roberson on 23 May 2013
4 is EOT (End of Transmission), control-D, which is used interactively on Unix-type systems to indicate end-of-file, but is never used as end-of-file when stored in files on Unix-type systems.
MS Windows uses ^Z (26) interactively to indicate end-of-file, and MS-DOS also used it stored in text files to indicate end-of-file. MS Windows reading in text mode (where the line terminator is CR LF pair) also often treat a stored ^Z as end-of-file; when MS Windows is not using that compatibility mode, there is no stored character that indicates end-of-file.
^Z as end-of-file is historical, and goes back to DEC's RSX-11 and to CPM, and I do not know were else. Ah, Wikipedia says it was borrowed from CPM.

Sign in to comment.


Jan
Jan on 23 May 2013
Edited: Jan on 23 May 2013
I've been very surprised, that there is in fact no method to reduce the size of the file in Matlab except for:
Create a new file
Copy the contents until the wanted end
Delete the old file
Rename the new file
Under Matlab 6.5 this was horribly slow, because these commands have been forwarded to DOS calls.
Finally I had done this by a C-Mex: FEX: FileResize
  1 Comment
Walter Roberson
Walter Roberson on 23 May 2013
The truncate() call is part of POSIX, not part of the C library, and MATLAB tends to omit functionality that is not part of the C library (even though they no longer support any non-POSIX system)
MATLAB also messes up the POSIX behavior for extending a file by fseek() to after the end of file and writing there; the fseek() stops at end of file instead.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!