Save file to disc using MSX-DOS calls

By fr3nd

Expert (110)

fr3nd's picture

22-04-2018, 11:16


I'm programming a text editor in SDCC and when saving an already existing file, when the file size of the saved file is smaller than the existing one, the size on the disk doesn't change.

For example, I open a 10k text file, delete most part of it, save it and exiting the editor. When I check the file size with "dir", the size of the file is still 10k. If check the content using "type", the content is the correct one (without the deleted lines), but when I open it with my own editor, the full file appears...

To save the file, I'm writing it to disk using _WRITE (49H), ending it with the char #1A, and closing it with _CLOSE (45H)...

Am I missing anything here? Is there anything I need to do to change the ending of the file?

If you want the check the code, it's available here:

Login or register to post comments

By hit9918

Prophet (2897)

hit9918's picture

22-04-2018, 13:48

a write with 0 byte length does it.
but you seem to use DOS2 functions, I dont know.

By Grauw

Ascended (9377)

Grauw's picture

22-04-2018, 21:03

You could use _CREATE to open the file, then it will replace the previous one.

The DOS1 function _WRBLK has a function to reduce the size of an existing file:


If the number of records to write (HL) is zero then no data will be written, but the size of the file will be altered to the value specified by the random record field. This may be either longer or shorter than the file's current size and disk space will be allocated or freed as required. Additional disk space allocated in this way will not be initialized to any particular value.

But unfortunately I can’t find a DOS2 equivalent… _WRITE will extend the file size but not shrink, and also does not accept a size of 0 so you always need to write a dummy byte (I do this in Gunzip to speed up file writing). So I’m afraid you either have to use the _CREATE approach or mess around with FCBs and their directory woes.

By fr3nd

Expert (110)

fr3nd's picture

22-04-2018, 19:38

Using _CREATE instead of _OPEN fixed it Smile

Thanks for your help!