Review of File Handling using win32

The CreateFile() function opens a file for read and/or write access. The CreateFile() function also opens mail slots, named pipes, communication ports, etc. The function returns a HANDLE to the desired file. The CreateFile() function takes seven arguments, which we have looked at in detail in earlier posts.

#include <windows.h>
#include <stdio.h>

int main(void){
	
	HANDLE newFile;

	newFile = CreateFile(
		"newfile.txt",
		GENERIC_READ | GENERIC_WRITE,
		0, //no sharing
		0, //inherit security
		CREATE_ALWAYS,
		0, //file flags
		0
		);

	if(newFile!=INVALID_HANDLE_VALUE){
		printf("New file created.\n");
 	} else {
		printf("New file not created.\n");
	}

	return 0;

}

Note that the CreateFile() function returns a handle on success, or INVALID_HANDLE_VALUE.

The CloseHandle() function closes file handles. Generally speaking, it’s a good idea to explicitly close all file handles. The CloseHandle() function returns a Boolean value indicated whether or not the file handle was closed properly.

#include <windows.h>
#include <stdio.h>

int main(void){
	
	HANDLE hFile = CreateFile(
		"textfile.txt",
		GENERIC_WRITE,
		FILE_SHARE_READ,
		0,//inherit security
		CREATE_NEW, //create only if no previous file
		0,
		0
		);

	if(hFile != INVALID_HANDLE_VALUE){
		printf("File created successfully.\n");
 	} else {
		printf("File not created successfully.\n");
		exit(EXIT_FAILURE);
 	}

	if(CloseHandle(hFile)){
		printf("File handle closed.\n");
	} else {
		printf("File handle not closed.\n");
	}
 
	return 0;

}

The WriteFile() function takes five arguments. The first argument is the handle to a file that has already been opened. The second argument is the buffer to be written to the file. The third argument is the number of bytes to write; typically this is the size of the buffer, in bytes, not characters. The fourth argument is a pointer to a DWORD variable that will store the number of bytes

#include <windows.H>
#include <stdio.h>

int main(void){
	
	char *szFilename = "textfile.txt";
	char *szSpaceGhost = "Are you getting enough oxygen?";
	DWORD dwBytesWritten;
	HANDLE hWriteFile;
	BOOL bValue;

	hWriteFile = CreateFile(
		szFilename,
		GENERIC_WRITE,
		0,
		0,
		CREATE_ALWAYS,
		0,
		0
		);

	if(hWriteFile!=INVALID_HANDLE_VALUE){
		printf("File handle opened for %s\n", szFilename);
	} else {
		printf("Error opening file %s\n", szFilename);
		printf("Error %d\n", GetLastError());
	}

	//WriteFile() function returns a Boolean value
	bValue = WriteFile(hWriteFile, szFilename, strlen(szFilename), &dwBytesWritten, NULL);

	if(bValue){
		printf("File %s written to successfully.\n", szFilename);
	}

	if(CloseHandle(hWriteFile)){
		printf("File handle closed.\n");
	}

	return 0;

}

The ReadFile() function is a block-reading function. A buffer is passed to ReadFile() as well as the number of bytes to be read, and the function retrieves that number of bytes from the file starting at the current offset. As with WriteFile(), the first argument is a handle to an open file.

#include 
#include 

int main(void){
	
	char *szFilename = "textfile.txt";
	char *szWriteBuffer = "That is mahogany!";
	char szReadBuffer[2056];
	DWORD dwBytesRead, dwBytesWritten;

	HANDLE hFile = CreateFile(
		szFilename,
		GENERIC_WRITE,
		0,
		0,
		OPEN_EXISTING,
		0,
		0
		);

	if(hFile==INVALID_HANDLE_VALUE){
		printf("Error accessing file for writing.\n");
		exit(EXIT_FAILURE);
	}

	if(WriteFile(hFile, szWriteBuffer, strlen(szWriteBuffer) * sizeof(char), &dwBytesWritten,NULL)){
		printf("File %s written to, %d bytes.\n", szFilename, dwBytesWritten);
	}

	//close the handle
	CloseHandle(hFile);

	hFile = CreateFile(
		szFilename,
		GENERIC_READ,
		0,
		0,
		OPEN_EXISTING,
		0,
		0
		);

	if(hFile==INVALID_HANDLE_VALUE){
		printf("Error accessing file for reading.\n");
		exit(EXIT_FAILURE);
	}

	if(ReadFile(hFile, szReadBuffer, sizeof(szReadBuffer), &dwBytesRead, NULL)){
		printf("File %s read from, %d bytes.\n", szFilename, dwBytesRead);
	} else {
		printf("Error reading form file.\n");
		exit(EXIT_FAILURE);
	}

	//append termination character to szReadBuffer;
	//using dwBytesRead value set in the ReadFile() function
	szReadBuffer[dwBytesRead] = '';

	printf("%s\n", szReadBuffer);

	return 0;

}

We will cover accessing file metadata and review copying and moving files at a later date. If you get the chance, take a look at my book on C at http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M/.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s