c++ - Weird characters in a std::vector<TCHAR*> -
trying sub directories, , files in sub directories, , i'm passing std::vector reference function gets of directories. can cout cfilename inside function once returns main vector has weird characters.
currently have character set use unicode. when using multibyte print weird characters, prints nothing. vector have in (directories.size() returns value > 0)
can't think of else. question lol. thanks
#include <iostream> #include <vector> #include <windows.h> #include <tchar.h> #include <stdio.h> // declare function prototypes dword listdirectories(std::vector<tchar*>&); // global variable holds current working path of program tchar buffer[max_path]; void main() { // declare variables, dwcount return value listdirectories, directories stores sub directories, cdirectory stores current directory dword dwcount; //std::vector<tchar*> directories; std::vector<tchar*> directories; tchar cdirectory[max_path]; // current directory getcurrentdirectory(max_path, buffer); // set cdirectory (current directory) buffer (atm current directory) + add \\ end , make working directory _tcscpy_s(cdirectory, buffer); _tcscat_s(cdirectory, l"\\*"); // dwcount count of how many directories found, used later dwcount = listdirectories(directories); // range loop print each value in std::vector<tchar*> directories (auto tstr : directories) { // doing wcout here prints weird characters. std::wcout << tstr << std::endl; } std::cin.get(); } // end void main() dword listdirectories(std::vector<tchar*> &directories) { // declare variables, count used number of directories, hfind findfirstfile, data used store file data dword count = 0; handle hfind = invalid_handle_value; win32_find_data data; tchar currentdir[max_path]; // copy current working directory currentdir, used subdir _tcscpy_s(currentdir, buffer); // append "\\*" buffer make working directory _tcscat_s(buffer, l"\\*"); // find first file in current working directory, storying data in data reference hfind = findfirstfile(buffer, &data); // if hfind not invalid handle if (hfind != invalid_handle_value) { // go through each file in directory { // if file attributes directory if (data.dwfileattributes & file_attribute_directory) { // create sub directory tchar subdir[max_path]; // fill subdir current directory + "\\" + dir name _tcscpy_s(subdir, currentdir); _tcscat_s(subdir, l"\\"); _tcscat_s(subdir, data.cfilename); // fill subdir current directory + "\\" + dir name //sprintf_s(subdir, "%s%s%s", currentdir, "\\", data.cfilename); // add directory directories (std::vector<tchar*>) if count > 1, because don't want "." , ".." directories if (count > 1){ directories.push_back(subdir); // doing wcout here prints subdir fine , works std::wcout << subdir << std::endl; } // add 1 count, used return how many directories found in current directory count++; } } while (findnextfile(hfind, &data) != 0); } // return count of directories found. -2 rid of "." , ".." directories return (count - 2); } // end dword listdirectories(std::vector<tchar*>&)
in listdirectories()
function
tchar subdir[max_path]; ... directories.push_back(subdir);
subdir
array local do ... while
loop within function , lifetime ends every iteration of loop.
change std::vector<tchar*> directories;
std::vector<std::basic_string<tchar>> directories;
, code should work correctly. you'll make copy of directory name , add vector
.
another alternative forget tchar
stuff , use wide character versions of when dealing windows api.
std::vector<std::wstring> directories; wchar_t cdirectory[max_path]; ... hfind = findfirstfilew(buffer, &data);
and forth
Comments
Post a Comment