Writing to a File

Windows CE 3.0

Use the WriteFile function to place data into a file. Like the ReadFile function, WriteFile uses the handle that is returned from the CreateFile function in the hFile parameter to identify the file to which to write. WriteFile then copies a specified number of bytes from the buffer that is pointed to by the lpBuffer parameter into the specified file. CreateFile begins placing the buffered data at the location within the file that is pointed to by the file pointer. Like ReadFile, WriteFile does not perform any formatting on the data; WriteFile writes the data exactly as the data exists in the buffer.

WriteFile does not support asynchronous write operations.

Note   Accessing the data buffer while Windows CE is performing a read or write operation on that buffer might lead to data corruption. Therefore, ensure that you do not modify a data buffer that is currently being read or written to.. If you use multiple threads or semaphores, ensure that one thread does not access the data buffer while another thread performs a read or write operation.

When writing to a file, use the FlushFileBuffers function to ensure that all data is written properly from the data buffer to the file. This situation is common when writing to a file on an installed file system, such as the FAT file system.

To truncate the file, call the SetEndOfFile function. SetEndOfFile truncates the file at the current location of the pointer, and then it closes the file.