14.5.4 Using File Input and Output

Use the Windows OpenFile function to create, open, reopen, or delete a file. OpenFile returns an MS-DOS file handle that you can use with such C run-time functions as read, write, lseek, and close. If you compile your C module by using the small or medium memory model, the buffer parameter of read and write is a near pointer (char near *). If you want to read to or write from a buffer declared in your application as a far pointer or array, use the Windows functions _lread and _lwrite, which are particularly useful for reading into or writing out of dynamically allocated global memory objects. You can also use buffered file-input-and-output C functions, such as fopen, fread, and fwrite.

You can also use the Windows functions _lopen and _lcreat to create or open a file.

Since Windows is a multitasking system, other applications may attempt to access the same file that your application is reading to or writing from. You can control access by other applications when your application opens a file by setting the appropriate share bit in the wStyle parameter. Your application should leave files open only while it is reading to and writing from them, unless your application must control access to the file at other times.

Note:

If a dynamic-link library opens a file, the file handle belongs to the application that called the dynamic-link library. If the library opens more than one file on behalf of multiple applications, it is possible that MS-DOS will assign the same file-handle value more than once.