Working with Temporary Files Using win32

To retrieve the designated directory for temporary files, we can use the GetTempPath() function. The function takes two arguments, the first argument indicates the size of the buffer to store the path in TCHARs, and the second argument is the buffer to store the path itself.

The GetTempFileName() function generates a unique file name, with the .tmp suffix, in a specified directory, and optionally will create the file. The function takes four arguments. The first argument is the directory for the temporary file. We can use the GetTempPath() function to fetch the path to to the directory for temporary files, or we can use the current directory by entering a period here. The second argument is the prefix of the temporary name; it should be three characters or less. The third argument is usually set to zero. If the value is not zero, the file will not be created. The final argument is the buffer that receives the temporary file name. The length of this buffer should at the least be set to MAX_PATH.

#include "stdafx.h"
#include <Windows.h>

int main()
{
 TCHAR tchTmpPath[MAX_PATH];
 TCHAR tchTmpBuffer[MAX_PATH];

 //GetTempPath() returns nonzero on success
 //size is in TCHARs
 if (GetTempPath(MAX_PATH, tchTmpPath) == 0) {
 printf("Error getting temporary file directory.\n");
 exit(EXIT_FAILURE);
 }

 //returns 0 on failure
 if (GetTempFileName(tchTmpPath, _T("TMP"), 0, tchTmpBuffer) == 0) {
 printf("Error getting temporary file name.\n");
 exit(EXIT_FAILURE);
 }

 _tprintf(_T("Temporary file created: %s\n"), tchTmpBuffer);

 return 0;
}

For our next example, we will implement a variant of the *nix touch command that updates the file access and modification time of all the files in the directory to the current system time. We will use the FindFirstFile() and FindNextFile() functions to list all the files in the current working directory.

The GetSystemTimeAsFileTime() function retrieves the current system date and time and stores it in a FILETIME structure.

#include "stdafx.h"
#include <Windows.h>

int UpdateFileTime(const TCHAR *tchFileNamePtr);

int main()
{
	WIN32_FIND_DATA structWIN32Data;
	HANDLE hSearchFile;

	hSearchFile = FindFirstFile(_T("*"), &structWIN32Data);

	if (hSearchFile == INVALID_HANDLE_VALUE) {
		printf("Error getting search handle.\n");
		exit(EXIT_FAILURE);
	}
	//use do-while loop to loop through 
        //all files in the current directory
	do {
		if (UpdateFileTime(structWIN32Data.cFileName) > 0) {
			_tprintf(_T("Error updating %s\n"), structWIN32Data.cFileName);
		}
	} while (FindNextFile(hSearchFile, &structWIN32Data));

    return 0;
}

int UpdateFileTime(const TCHAR *tchFileNamePtr) {
	FILETIME structFT;
	HANDLE hFile;

	_tprintf(_T("Updating file time for %s\n"), tchFileNamePtr);
	//retrieves the current date and time
	GetSystemTimeAsFileTime(&structFT);
	hFile = CreateFile(tchFileNamePtr, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	//could not get handle?
	if (hFile == INVALID_HANDLE_VALUE) {
		return GetLastError();
	}

	//change the file time
	SetFileTime(hFile, NULL, &structFT, &structFT);

	//close the file handle
	CloseHandle(hFile);

	return 0;

}

File locking is a limited method of managing concurrency between process and threads.

Windows can lock files so that no other process, or thread within the process, can access the locked region of the file. File locks can be shared or exclusive. Any attempt to write to the locked region, or to obtain a conflicting lock, will fail.

The LockFileEx() function locks a byte range in an open file for either shared or exclusive access. The first argument to the function is the handle of an open file, which must at least have GENERIC_READ access. The second argument is a flag that determines the lock mode and whether to wait for the lock to become available.

 #include "stdafx.h"
#include <Windows.h>

int main()
{
 //exclusive, read-write lock (default is write lock)
 printf("LOCKFILE_EXCLUSIVE_LOCK = %d\n", LOCKFILE_EXCLUSIVE_LOCK);
 
 //return immediately with FALSE if lock can't be 
 //acquired
 printf("LOCKFILE_FAIL_IMMEDIATELY = %d\n", LOCKFILE_FAIL_IMMEDIATELY);

 return 0;
}

We’ll look at file locking in greater depth at another time.

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