0

I need help combining the file name with the filepath
my code:

WIN32_FIND_DATA FindData;
    HANDLE hFind;

    hFind = FindFirstFile(L"../art/*.dds", &FindData );

    if( hFind == INVALID_HANDLE_VALUE )
    {
        PrintCharS("Error searching directory");
        return;
    }

    do
    {

        char ch[260];
        char DefChar = ' ';
        WideCharToMultiByte(CP_ACP, 0, FindData.cFileName, -1, ch, 260, &DefChar, NULL);

        string ss(ch);

        if (ch != "invalid.dds")
        {

            WCHAR* path = L"../art/";
            //path = path + (WCHAR)ch;

            int size = strlen(ch);
            wchar_t* str;
            size_t length = 0; 
            mbstowcs_s(&length, str, ch, size+1);

            WCHAR* Path = path + str;

            PrintWCHARS(Path);

        }

    }
    while( FindNextFile(hFind, &FindData) > 0 );

All I would like is to get a WCHAR* with the filepath AND filename

3
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by vijayan121
Featured Replies
  • 1

    The Microsoft implementation includes the draft TR2 filesystem library. (With other compilers, `boost::filesystem` which privides like functionality can be used.) #include <iostream> #include <string> #include <vector> #include <filesystem> namespace fs = std::tr2::sys ; std::vector<std::wstring> files_in_directory( fs::wpath path = L".", bool recursive = true ) { std::vector<std::wstring> files ; try { … Read More

0

Use wcscpy_s or wcscpy for widechars, which correspond to strcpy_s and strcpy for chars.

Note also you need buffers for these functions.

1

The Microsoft implementation includes the draft TR2 filesystem library.
(With other compilers, boost::filesystem which privides like functionality can be used.)

#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
namespace fs = std::tr2::sys ;

std::vector<std::wstring> files_in_directory( fs::wpath path = L".", bool recursive = true )
{
    std::vector<std::wstring> files ;

    try
    {
        if( fs::exists(path) ) files.push_back( fs::system_complete(path) ) ;

        if( fs::is_directory(path) )
        {
            using iterator = fs::basic_directory_iterator < fs::wpath > ;
            for( iterator iter(path) ; iter != iterator() ; ++iter )
            {
                files.push_back( fs::system_complete( iter->path() ) ) ;
                if( recursive ) 
                    for( const std::wstring& p : files_in_directory( iter->path() ) )
                        files.push_back( std::move(p) ) ;
            }
        }
    }

    catch( const std::exception& ) { /* error */ }

    return files ;
}

int main()
{
    for( const std::wstring& path : files_in_directory( L"C:\\Windows", false ) ) 
        std::wcout << path << '\n' ;
}

http://rextester.com/MVQND35610

Votes + Comments
Very Nice
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.