2012年3月30日 星期五

Get full file path from current directory

We have to get full file path from current directory if we are using CreateFile api to open a file.


    wchar_t* fileName = L"\\test.txt";

    // get current directory
    char currentDirectory[_MAX_PATH];  
    getcwd(currentDirectory, MAX_PATH);

    // convert from char to wchar_t
    const size_t size = sizeof(currentDirectory) / sizeof(char);
    wchar_t filePath[size];
    MultiByteToWideChar(CP_ACP, 0, currentDirectory, size, filePath, size);

    // concatenate file path and file name
    wchar_t* fileFullPath = new wchar_t[sizeof(filePath)+sizeof(fileName)+1];
    wcscpy(fileFullPath, filePath);
    wcscat(fileFullPath, fileName);
    delete[] fileFullPath;

Then, you can follow the example below to open the file.
http://twnin.blogspot.com/2012/03/win32-api-read-txt-file.html

沒有留言:

張貼留言