http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/creating_and_opening_files.asphar nogle eksempler:
Example: Open a File for Reading
The following code fragment uses CreateFile to open an existing file for reading. Note that ErrorHandler is a placeholder for a user-defined function that displays an error message and exits the code.
HANDLE hFile;
hFile = CreateFile("MYFILE.TXT", // open MYFILE.TXT
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorHandler("Could not open file."); // process error
}
In this case, CreateFile succeeds only if a file named Myfile.txt already exists in the current directory. An application should check the return value of CreateFile before attempting to use the handle to access the file. If an error occurs, the application should use the GetLastError function to get extended error information and respond accordingly.
Example: Open a File for Writing
The following code fragment uses CreateFile to create a new file and opens it for writing.
HANDLE hFile;
hFile = CreateFile("MYFILE.TXT", // create MYFILE.TXT
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // no security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL | // normal file
FILE_FLAG_OVERLAPPED, // asynchronous I/O
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorHandler("Could not open file."); // process error
}