2.7. File Reading and Writing

Section 2.3 briefly introduced simple file reading using CreateFile, ReadFile, and CloseHandle. In this section we will examine file seeking, reading, and writing in more detail, and look at the CreateFile function more carefully. The operations here are all synchronous, so they block until complete. Section 2.8 discusses asynchronous file operations.

Listing 2.3 contains a program that performs a simple file read, designed for a text file or a file of bytes. Listing 2.13 demonstrates a file-write operation that writes structures to a new file.

Code Listing 2.13. Writing structures to a file
 // filewrit.cpp #include <windows.h> #include <iostream.h> typedef struct { int a, b, c; } data; void main() { HANDLE fileHandle; ...

Get Win32 System Services: The Heart of Windows® 98 and Windows® 2000 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.