Note that there are some explanatory texts on larger screens.

plurals
  1. POWin32 API storing results in vector filename is wrong
    text
    copied!<p>I'm trying to store WIN32_FIND_DATA results in a vector of my own structs (File and Directory). After that I'm trying to loop through the vectors and print the filename however this doesn't work as I'd expected. The filename is displayed as "IIIIIIIIIIIIIIIIIIIIIIII" (or something like that) and the path is empty but only when looping through the vectors. The rest of the code works fine.</p> <p>This is my code:</p> <pre><code>#define WIN32_LEAN_AND_MEAN #include &lt;windows.h&gt; #include &lt;iostream&gt; #include &lt;tchar.h&gt; #include &lt;string.h&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; #define ERRMSGBUFFERSIZE 256 using namespace std; //Library void fnDisplayError(DWORD dwErrorMsgId) { DWORD ret; HINSTANCE hInst; HLOCAL pBuffer; ret = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorMsgId, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&amp;pBuffer, ERRMSGBUFFERSIZE, NULL ); _tprintf( _TEXT("%s"), (LPTSTR)pBuffer ); // Free the buffer. LocalFree( pBuffer ); } WCHAR* ToWchar(char* chr) { WCHAR *retStr; int nChars = MultiByteToWideChar(CP_ACP, 0, chr, -1, NULL, 0); retStr = new WCHAR[nChars]; MultiByteToWideChar(CP_ACP, 0, chr, -1, (LPWSTR)retStr, nChars); return retStr; } CHAR wton (WCHAR w) { return CHAR(w); } char* ToStr(WCHAR chr[260]) { char retStr[260]; transform(chr, chr+260, retStr, wton); return retStr; }; //Structures struct File { char* name; char* path; File(WIN32_FIND_DATA fileData, char* _path) { name = ToStr(fileData.cFileName); path = _path; //The data here is correct when inspecting the variables after setting a breakingpoint. } }; struct Directory { char* name; char* path; Directory(WIN32_FIND_DATA fileData, char* _path) { name = ToStr(fileData.cFileName); path = _path; //The data here is correct when inspecting the variables after setting a breakingpoint. } }; struct FindResults { vector&lt;File&gt; files; vector&lt;Directory&gt; folders; void addFile(File file) { files.push_back(file); } void addFolder(Directory file) { folders.push_back(file); } }; //Application library FindResults GetFiles(char* path) { WIN32_FIND_DATA FindFileData; HANDLE hFind; FindResults results = FindResults(); LPWSTR realPath = ToWchar(path); hFind = FindFirstFile(realPath, &amp;FindFileData); if (hFind == INVALID_HANDLE_VALUE) { fnDisplayError(GetLastError()); return results; } else { while(FindNextFile(hFind, &amp;FindFileData) != 0) { if (FindFileData.dwFileAttributes &amp; FILE_ATTRIBUTE_DIRECTORY) { results.addFolder(Directory(FindFileData, path)); } else { results.addFile(File(FindFileData, path)); } } } FindClose(hFind); delete [] realPath; return results; } void RecursiveFind(char* path, int max_lvl = 10, int lvl = 1) { FindResults results = GetFiles((char*)string(path).append("*").c_str()); if (lvl &gt; max_lvl) return; //Loop through folders for (vector&lt;Directory&gt;::iterator it = results.folders.begin(); it != results.folders.end(); ++it) { printf("&lt;DIR&gt; %s\n", it-&gt;path); //The values are wrong here.. } //Loop through files for (vector&lt;File&gt;::iterator it = results.files.begin(); it != results.files.end(); ++it) { printf("&lt;FILE&gt; %s\n", it-&gt;path); //The values are wrong here.. } } void main() { RecursiveFind("C:\\"); } </code></pre> <p><strong>EDIT</strong></p> <p>I still don't know how to cast a CHAR to a WCHAR and vise versa since I think that is my problem. Thanks for everyones help but for now I'm going to learn C++ a bit more before I continue writing this program.</p> <p>The problem was (at least I think so) the casting from WCHAR to CHAR and vice versa.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload