Write End Of File (EOF)

33 次查看(过去 30 天)
Julio
Julio 2013-5-23
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?

回答(3 个)

Walter Roberson
Walter Roberson 2013-5-23
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 2013-5-23
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 个评论
Jan
Jan 2013-5-23
As Walter has pointed out already: This works only under Windows and when the file is opened in text mode.
Walter Roberson
Walter Roberson 2013-5-23
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.

请先登录,再进行评论。


Jan
Jan 2013-5-23
编辑:Jan 2013-5-23
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 个评论
Walter Roberson
Walter Roberson 2013-5-23
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by