CreateFile(), GetTempPath(), GetTempFileName(), WriteFile(), ReadFile(), MoveFileEx(), CloseHandle()

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged

To do: Another file manipulations

To show: Using CreateFile(), GetTempPath(), GetTempFileName(), WriteFile(), ReadFile(), MoveFileEx(), CloseHandle() C functions

 

 

#define _WIN32_WINNT 0x0501

#include <windows.h>

#include <stdio.h>

#include <tchar.h>

#define BUFSIZE 65536

 

int main(void)

{

HANDLE hFile;

HANDLE hTempFile;

DWORD dwRetVal;

DWORD dwBytesRead;

DWORD dwBytesWritten;

DWORD dwBufSize = BUFSIZE;

UINT uRetVal;

TCHAR szTempName[BUFSIZE] = TEXT("just_dummy");

LPWSTR buffer = malloc(sizeof(BUFSIZE));

TCHAR lpPathBuffer[BUFSIZE] = TEXT("just_dummy");

BOOL fSuccess;

 

// Open the existing file. Make sure the file is there

hFile = CreateFile(L"C:\\original.txt", // file name

GENERIC_READ, // open for reading

0, // do not share

NULL, // default security

OPEN_EXISTING, // existing file only

FILE_ATTRIBUTE_NORMAL, // normal file

NULL); // no template

 

if (hFile == INVALID_HANDLE_VALUE)

{

printf ("CreateFile() failed with error %d.\n", GetLastError());

return (1);

}

else

printf ("CreateFile() is OK\n");

 

// Get the temp path.

dwRetVal = GetTempPath(dwBufSize, // length of the buffer

lpPathBuffer); // buffer for path

if (dwRetVal > dwBufSize)

{

printf("GetTempPath() failed with error %d.\n", GetLastError());

return (2);

}

else

{

printf("GetTempPath() is OK.\n");

 

printf("The temp path is %S\n", lpPathBuffer);

}

 

// Create a temporary file.

uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files

L"NEW", // temp file name prefix

0, // create unique name

szTempName); // buffer for the name

 

if (uRetVal == 0)

{

printf ("GetTempFileName() failed with error %d.\n", GetLastError());

return (3);

}

else

{

printf("GetTempFileName() is OK.\n");

printf("The temp file name is %S.\n", szTempName);

}

 

// Create the new file to write the upper-case version to.

hTempFile = CreateFile((LPTSTR) szTempName, // file name

GENERIC_READ | GENERIC_WRITE, // open r-w

0, // do not share

NULL, // default security

CREATE_ALWAYS, // overwrite existing

FILE_ATTRIBUTE_NORMAL,// normal file

NULL); // no template

 

if (hTempFile == INVALID_HANDLE_VALUE)

{

printf ("\nCreateFile() failed with error %d.\n", GetLastError());

return (4);

}

else

printf("CreateFile() is OK.\n");

 

// Read BUFSIZE blocks to the buffer. Change all characters in the buffer to upper case. Write the buffer to the temporary file.

do

{

if (ReadFile(hFile,

buffer,

BUFSIZE,

&dwBytesRead,

NULL))

{

CharUpperBuff(buffer, dwBytesRead);

fSuccess = WriteFile(hTempFile,

buffer,

dwBytesRead,

&dwBytesWritten,

NULL);

if (!fSuccess)

{

printf ("WriteFile() failed with error %d.\n", GetLastError());

return (5);

}

else

printf("WriteFile() is OK.\n");

}

else

{

printf ("ReadFile() failed with error %d.\n", GetLastError());

return (6);

}

} while (dwBytesRead == BUFSIZE);

// Close the handles to the files.

fSuccess = CloseHandle (hFile);

if (!fSuccess)

{

printf ("CloseHandle() failed with error %d.\n", GetLastError());

return (7);

}

else

printf("CloseHandle() is OK.\n");

 

fSuccess = CloseHandle (hTempFile);

if (!fSuccess)

{

printf ("CloseHandle() failed with error %d.\n", GetLastError());

return (8);

}

else

printf("CloseHandle() is OK.\n");

 

// Move the temporary file to the new text file.

fSuccess = MoveFileEx(szTempName,

L"allcaps.txt",

MOVEFILE_REPLACE_EXISTING);

if (!fSuccess)

{

printf ("MoveFileEx() failed with error %d.\n", GetLastError());

return (9);

}

else

printf("MoveFileEx() is OK.\n");

return (0);

}

 

Output example:

 

CreateFile() is OK

GetTempPath() is OK.

The temp path is C:\DOCUME~1\Johnny\LOCALS~1\Temp\

GetTempFileName() is OK.

The temp file name is C:\DOCUME~1\Johnny\LOCALS~1\Temp\NEW19.tmp.

CreateFile() is OK.

ReadFile() failed with error 998.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index