Trying to get a list of all directories in the current directory (and eventually directories within all directories, within directories, within directories, etc) and then get a list of all files in directories. Problem is, I'm using TCHAR[] for my buffer, working just fine, when I go to my listDirectories function, which stores all found directories in a std::vector<TCHAR*> it stores weird characters.
I can do a std::wcout << buffer << std::endl; and it will print the proper directory fine. I suspect it's my vector doing some bad conversion but TCHAR[] to TCHAR* shouldn't have any weird conversions. But, what do I know. I haven't done really any windows coding.
Code:
#include <iostream>
#include <vector>
#include <Windows.h>
#include <tchar.h>
// Declare function prototypes
DWORD listDirectories(std::vector<TCHAR*>&);
// Global variable that holds the current working path of the program
TCHAR buffer[MAX_PATH];
void main()
{
// Declare variables, dwCount for return value from listDirectories, directories stores all sub directories, cDirectory stores current directory
DWORD dwCount;
std::vector<TCHAR*> directories;
TCHAR cDirectory[MAX_PATH];
// Get current directory
GetCurrentDirectory(MAX_PATH, buffer);
// Set cDirectory (current directory) to buffer (ATM is current directory) + add \\ to the end and make it the working directory
_tcscpy_s(cDirectory, buffer);
_tcscpy_s(cDirectory, "\\");
// dwCount is count of how many directories found, to be used later
dwCount = listDirectories(directories);
// Range for loop to print each value in the std::vector<TCHAR*> directories
for (auto tStr : directories) {
// std::cout and std::wcout print the same weird characters
std::wcout << tStr << std::endl;
}
std::cin.get();
} // end void main()
DWORD listDirectories(std::vector<TCHAR*> &directories)
{
// Declare variables, count used for number of directories, hFind for FindFirstFile, data used to store file data
DWORD count = 0;
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA data;
// Append "\\*" to buffer to make it a working directory
_tcscat_s(buffer, "\\*");
// Find first file in the current working directory, storying data in data as a reference
hFind = FindFirstFile(buffer, &data);
// If hFind is not an invalid handle
if (hFind != INVALID_HANDLE_VALUE) {
// Go through each file in the directory
do
{
// If the file attributes is a directory
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
// Create a sub directory
TCHAR subDir[MAX_PATH];
// Fill subDir with current directory + "\\" + dir name
sprintf_s(subDir, "%s%s%s", currentDir, "\\", data.cFileName);
// Add directory to my directories (std::vector<TCHAR*>)
directories.push_back(data.cFileName);//data.cFileName);
// Add 1 to count, used as the return for how many directories found in the current directory
count++;
}
} while (FindNextFile(hFind, &data) != 0);
}
// Return count of directories found
return count;
} // end DWORD listDirectories(std::vector<TCHAR*>&)