Creating and Opening Files

All types of files are created and opened with one function in the Win32 API, CreateFile. An application can specify whether it will read from the file, write to the file, or both. It can also specify whether it wants to share the file for reading, writing, both, or neither. Listing 17.1 shows a code segment that opens an existing file or creates a new file if it does not exist. After opening the file, you can use it to perform read and write operations.

Code Listing 17.1. Opening and Closing a File
 HANDLE hFile; // Open/create the file. //...................... hFile = CreateFile( "FILE1.TXT", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); if ( hFile != INVALID_HANDLE_VALUE ...

Get Microsoft Windows 2000 API SuperBible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.