CreateFile(), GetTempFileName(), ReadFile(), CloseHandle(), MoveFile()

 

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: Various file manipulations

To show: CreateFile(), GetTempFileName(), ReadFile(), CloseHandle(), MoveFile()

 

 

#include <windows.h>

#include <stdio.h>

 

int main(void)

{

HANDLE hFile;

HANDLE hTempFile;

DWORD dwBytesRead, dwBytesWritten;

// We need to allocate memory for those variables initially. Previous Microsoft compiler does not need

// this for the default project setting, else you will be penalized by the run-time (check) error

LPWSTR szTempName = malloc(sizeof(szTempName));

LPWSTR buffer = malloc(sizeof(buffer));

LPCWSTR fname = L"c:\\mytestfile.txt";

 

// Open the existing file.

hFile = CreateFile(fname, //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("Could not open %S file.\n", fname);

}

else

printf("%S file opened successfully.\n", fname);

 

// Create a temporary file. Make sure is are c:\temp folder

GetTempFileName(L"c:\\temp", // directory for temp files

L"testmp", // temp file name prefix

0, // create unique name

szTempName); // buffer for name

 

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

GENERIC_READ | GENERIC_WRITE, // open for read/write

0, // do not share

NULL, // default security

CREATE_ALWAYS, // overwrite existing file

FILE_ATTRIBUTE_NORMAL, // normal file

NULL); // no attribute template

 

if(hTempFile == INVALID_HANDLE_VALUE)

printf("Could not create %S temporary file.\n", szTempName);

else

printf("%S temporary file created successfully.\n", szTempName);

 

// Read 2K blocks of the content to the buffer.

// Change all characters in the buffer to uppercase.

// Write the buffer to the temporary file.

printf("Reading the %S file, change to the uppercase and\n", fname);

printf("write to %S temporary file\n", szTempName);

do

{

if(ReadFile(hFile, buffer, 2048, &dwBytesRead, NULL))

{

CharUpperBuff(buffer, dwBytesRead);

WriteFile(hTempFile, buffer, dwBytesRead, &dwBytesWritten, NULL);

}

} while (dwBytesRead == 2048);

 

// Close both files' handles.

if(CloseHandle(hFile) != 0)

printf("%S handle closed successfully.\n", fname);

if(CloseHandle(hTempFile) != 0)

printf("%S handle closed successfully.\n", szTempName);

 

// Move the temporary file to the new text file

if(!MoveFile(szTempName, L"c:\\newfile.txt"))

printf("Could not move %S temp file.\n", szTempName);

else

printf("%S temp file moved successfully.\n", szTempName);

return 0;

}

 

Output example:

 

c:\mytestfile.txt file opened successfully.

c:\temp\tes53.tmp temporary file created successfully.

Reading the c:\mytestfile.txt file, change to the uppercase and

write to c:\temp\tes53.tmp temporary file

c:\mytestfile.txt handle closed successfully.

c:\temp\tes53.tmp handle closed successfully.

Could not move c:\temp\tes53.tmp temp file.

Press any key to continue . . .

 

 

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