Most MS-DOS programs carry out file operations through file-handle functions that use a unique 16-bit value, called a handle, to identify a file. The program receives a file handle when it opens or creates a file and uses the handle with subsequent functions to read from, write to, or carry out other operations on the file.
Programs can open existing files by using Open File with Handle (Interrupt 21h Function 3Dh). The program supplies a filename (or full path) and the type of file access required: read-only, write-only, or read-and-write. The function opens the file and returns a handle for reading from, writing to, and closing the file.
A program can read from a file opened for read access by using Read File or Device (Interrupt 21h Function 3Fh). Similarly, a program can write to a file opened for write access by using Write File or Device (Interrupt 21h Function 40h). When a program reads from or writes to a file, it specifies the number of bytes of data to be read or written and supplies the address of the buffer that contains or receives the data. A program can continue to read from a file until it reaches the end of the file; it can continue to write to a file until the file system has no more space available.
Every open file has a file pointer that specifies the next byte to read from the file or the next position to receive a byte written to the file. When a file is opened or created, the file pointer is set to zero, the beginning of the file. As a program reads from or writes to the file, the system moves the file pointer by the number of bytes read or written. When a program has read all bytes in a file, the file pointer moves to the end of the file and no further reading is possible. When a program writes to a file, the system writes over existing data unless the file pointer is at the end of the file, in which case the system appends the new data to the file and moves the file pointer to the new end of the file.
A program can move the file pointer by using Move File Pointer (Interrupt 21h Function 42h). The program must specify the amount to move and where to move from (beginning of file, end of file, or current position). The function moves the pointer and returns its new position relative to the beginning of the file.
When the number of bytes between the file pointer and the end of the file are fewer than the program requests, MS-DOS reads only to the end of the file. For example, if a program requests 512 bytes but only 250 bytes remain between the file pointer and the end of the file, only those 250 bytes are read. Read File or Device returns the number of bytes read, so that the program can determine how many bytes in its buffer are valid. Similarly, Write File or Device returns the number of bytes written, which may be fewer than requested if writing that number of bytes would exceed the maximum file size or if all available space on the storage medium has been used before the write operation is complete.
A program can truncate an existing file to zero bytes by using Create File with Handle and specifying the name of the existing file. (If the existing file is already open, however, Create File with Handle simply creates an additional handle for it.) To avoid unintentionally destroying existing files when creating new ones, a program should use Create New File (Interrupt 21h Function 5Bh), which returns an error value if the new filename matches an existing filename.
Programs often use temporary files for short-term storage and delete the files when no longer needed. A program can create temporary files with unique names by specifying a path for Create Temporary File (Interrupt 21h Function 5Ah), which then creates a file having a name that does not conflict with the name of any other file in that path.
Programs should close files when they are no longer needed. Leaving files open can cause loss or corruption of data if a system fails. A program can close a file by using Close File with Handle (Interrupt 21h Function 3Eh). If the program changed the file, MS-DOS updates the file's time and date and sets the archive attribute. MS-DOS closes any open files when a program terminates.