Last active
January 3, 2016 19:19
-
-
Save ytinasni/8507921 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define WIN32_ULTRA_LEAN | |
#define UNICODE | |
#include <Windows.h> | |
#include <cstdio> | |
#include <cstdlib> | |
int TestSetCurrentDirectory( const wchar_t* directory ) | |
{ | |
if( !SetCurrentDirectory( directory ) ) | |
{ | |
printf( " unable to set current directory.\n" ); | |
return 1; | |
} | |
wchar_t currentDirectory[MAX_PATH]; | |
if( !GetCurrentDirectory( MAX_PATH, currentDirectory ) ) exit(1); | |
wprintf( L"Current directory=%s\n", currentDirectory ); | |
} | |
int TestOpenFileRelative( const wchar_t* directory, const wchar_t* filename ) | |
{ | |
if( !SetCurrentDirectory( directory ) ) | |
{ | |
printf( " unable to set current directory.\n" ); | |
return 1; | |
} | |
HANDLE file = CreateFile( filename, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr ); | |
if( file == INVALID_HANDLE_VALUE ) | |
{ | |
printf( " unable to open file.\n" ); | |
return 1; | |
} | |
char buf[4096]; | |
DWORD numBytesRead; | |
if( !ReadFile( file, buf, sizeof(buf)-1, &numBytesRead, nullptr ) ) | |
return 1; | |
buf[numBytesRead] = 0; | |
CloseHandle( file ); | |
printf( " %s\n", buf ); | |
return 0; | |
} | |
int main( int, char** ) | |
{ | |
TestSetCurrentDirectory( L"C:\\windows" ); | |
TestSetCurrentDirectory( L"C:" ); | |
TestSetCurrentDirectory( L"C:\\" ); | |
TestSetCurrentDirectory( L"C:a" ); | |
TestSetCurrentDirectory( L"C:a" ); | |
TestSetCurrentDirectory( L"Z:" ); | |
TestSetCurrentDirectory( L"C:" ); | |
printf( "\n" ); | |
TestOpenFileRelative( L"C:", L"C:a\\b.txt" ); | |
TestOpenFileRelative( L"C:\\a", L"C:a\\b.txt" ); | |
TestOpenFileRelative( L"C:", L"C:a\\b.txt" ); | |
TestOpenFileRelative( L"Z:", L"C:a\\b.txt" ); | |
TestOpenFileRelative( L"Z:\\temp", L"C:a\\b.txt" ); | |
system( "PAUSE" ); | |
return 0; | |
} | |
// the files C:\a\b.txt and C:\a\a\b.txt exist and contain text describing their position on the file-system. | |
// Output: | |
/* | |
Current directory=C:\windows | |
Current directory=C:\windows | |
Current directory=C:\ | |
Current directory=C:\a | |
Current directory=C:\a\a | |
Current directory=Z:\ | |
Current directory=C:\ | |
This file is C:\a\b.txt | |
This file is C:\a\a\b.txt | |
This file is C:\a\a\b.txt | |
This file is C:\a\b.txt | |
This file is C:\a\b.txt | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment