Weird characters in a std::vector<TCHAR*> - c++

Trying to get all sub directories, and eventually all files in sub directories, and I'm passing a std::vector as a reference to a function that actually gets all of the directories. I can cout the cFileName inside the function but once it returns the main the vector has weird characters.
Currently have my Character Set to use Unicode. When I was using multibyte it would actually print weird characters, now it prints nothing. The vector does have something in it (directories.size() returns a value > 0)
Can't really think of anything else. Hopefully this was a good 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 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;
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);
_tcscat_s(cDirectory, L"\\*");
// 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) {
// 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 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;
TCHAR currentDir[MAX_PATH];
// Copy the current working directory into currentDir, will be used for subDir
_tcscpy_s(currentDir, buffer);
// Append "\\*" to buffer to make it a working directory
_tcscat_s(buffer, L"\\*");
// 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
_tcscpy_s(subDir, currentDir);
_tcscat_s(subDir, L"\\");
_tcscat_s(subDir, data.cFileName);
// Fill subDir with current directory + "\\" + dir name
//sprintf_s(subDir, "%s%s%s", currentDir, "\\", data.cFileName);
// Add directory to my directories (std::vector<TCHAR*>) if count > 1, because I don't want the "." and ".." directories
if (count > 1){
directories.push_back(subDir);
// Doing wcout here prints the subDir just fine and works properly
std::wcout << subDir << std::endl;
}
// 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. -2 to get rid of the "." and ".." directories
return (count - 2);
} // end DWORD listDirectories(std::vector<TCHAR*>&)

In your listDirectories() function
TCHAR subDir[MAX_PATH];
...
directories.push_back(subDir);
subDir is an array local to the do ... while loop within the function and its lifetime ends with every iteration of the loop.
Change std::vector<TCHAR*> directories; to std::vector<std::basic_string<TCHAR>> directories; and your code should work correctly. Now you'll make a copy of the directory name and add that to the vector.
Another alternative is to forget about all the TCHAR stuff and just use the wide character versions of everything when dealing with the Windows API.
std::vector<std::wstring> directories;
wchar_t cDirectory[MAX_PATH];
...
hFind = FindFirstFileW(buffer, &data);
and so forth

Related

C++ how to find file path of the file I am working on in my program [duplicate]

I want to create a file in the current directory (where the executable is running).
My code:
LPTSTR NPath = NULL;
DWORD a = GetCurrentDirectory(MAX_PATH,NPath);
HANDLE hNewFile = CreateFile(NPath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
I get exception at GetCurrentDirectory().
Why am I getting an exception?
I would recommend reading a book on C++ before you go any further, as it would be helpful to get a firmer footing. Accelerated C++ by Koenig and Moo is excellent.
To get the executable path use GetModuleFileName:
TCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName( NULL, buffer, MAX_PATH );
Here's a C++ function that gets the directory without the file name:
#include <windows.h>
#include <string>
#include <iostream>
std::wstring ExePath() {
TCHAR buffer[MAX_PATH] = { 0 };
GetModuleFileName( NULL, buffer, MAX_PATH );
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
return std::wstring(buffer).substr(0, pos);
}
int main() {
std::cout << "my directory is " << ExePath() << "\n";
}
The question is not clear whether the current working directory is wanted or the path of the directory containing the executable.
Most answers seem to answer the latter.
But for the former, and for the second part of the question of creating the file, the C++17 standard now incorporates the filesystem library which simplifies this a lot:
#include <filesystem>
#include <iostream>
std::filesystem::path cwd = std::filesystem::current_path() / "filename.txt";
std::ofstream file(cwd.string());
file.close();
This fetches the current working directory, adds the filename to the path and creates an empty file. Note that the path object takes care of os dependent path handling, so cwd.string() returns an os dependent path string. Neato.
GetCurrentDirectory does not allocate space for the result, it's up to you to do that.
TCHAR NPath[MAX_PATH];
GetCurrentDirectory(MAX_PATH, NPath);
Also, take a look at Boost.Filesystem library if you want to do this the C++ way.
An easy way to do this is:
int main(int argc, char * argv[]){
std::cout << argv[0];
std::cin.get();
}
argv[] is pretty much an array containing arguments you ran the .exe with, but the first one is always a path to the executable. If I build this the console shows:
C:\Users\Ulisse\source\repos\altcmd\Debug\currentdir.exe
IMHO here are some improvements to anon's answer.
#include <windows.h>
#include <string>
#include <iostream>
std::string GetExeFileName()
{
char buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
return std::string(buffer);
}
std::string GetExePath()
{
std::string f = GetExeFileName();
return f.substr(0, f.find_last_of( "\\/" ));
}
#include <iostream>
#include <stdio.h>
#include <dirent.h>
std::string current_working_directory()
{
char* cwd = _getcwd( 0, 0 ) ; // **** microsoft specific ****
std::string working_directory(cwd) ;
std::free(cwd) ;
return working_directory ;
}
int main(){
std::cout << "i am now in " << current_working_directory() << endl;
}
I failed to use GetModuleFileName correctly. I found this work very well.
just tested on Windows, not yet try on Linux :)
WCHAR path[MAX_PATH] = {0};
GetModuleFileName(NULL, path, MAX_PATH);
PathRemoveFileSpec(path);
Please don't forget to initialize your buffers to something before utilizing them. And just as important, give your string buffers space for the ending null
TCHAR path[MAX_PATH+1] = L"";
DWORD len = GetCurrentDirectory(MAX_PATH, path);
Reference
You should provide a valid buffer placeholder.
that is:
TCHAR s[100];
DWORD a = GetCurrentDirectory(100, s);
#include <windows.h>
using namespace std;
// The directory path returned by native GetCurrentDirectory() no end backslash
string getCurrentDirectoryOnWindows()
{
const unsigned long maxDir = 260;
char currentDir[maxDir];
GetCurrentDirectory(maxDir, currentDir);
return string(currentDir);
}
You can remove the filename from GetModuleFileName() with more elegant way:
TCHAR fullPath[MAX_PATH];
TCHAR driveLetter[3];
TCHAR directory[MAX_PATH];
TCHAR FinalPath[MAX_PATH];
GetModuleFileName(NULL, fullPath, MAX_PATH);
_splitpath(fullPath, driveLetter, directory, NULL, NULL);
sprintf(FinalPath, "%s%s",driveLetter, directory);
Hope it helps!
GetCurrentDirectory() gets the current directory which is where the exe is invoked from. To get the location of the exe, use GetModuleFileName(NULL ...). if you have the handle to the exe, or you can derive it from GetCommandLine() if you don't.
As Mr. Butterworth points out, you don't need a handle.
Why does nobody here consider using this simple code?
TCHAR szDir[MAX_PATH] = { 0 };
GetModuleFileName(NULL, szDir, MAX_PATH);
szDir[std::string(szDir).find_last_of("\\/")] = 0;
or even simpler
TCHAR szDir[MAX_PATH] = { 0 };
TCHAR* szEnd = nullptr;
GetModuleFileName(NULL, szDir, MAX_PATH);
szEnd = _tcsrchr(szDir, '\\');
*szEnd = 0;
I guess, that the easiest way to locate the current directory is to cut it from command line args.
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
std::string cur_dir(argv[0]);
int pos = cur_dir.find_last_of("/\\");
std::cout << "path: " << cur_dir.substr(0, pos) << std::endl;
std::cout << "file: " << cur_dir.substr(pos+1) << std::endl;
return 0;
}
You may know that every program gets its executable name as first command line argument. So you can use this.
Code snippets from my CAE project with unicode development environment:
/// #brief Gets current module file path.
std::string getModuleFilePath() {
TCHAR buffer[MAX_PATH];
GetModuleFileName( NULL, buffer, MAX_PATH );
CT2CA pszPath(buffer);
std::string path(pszPath);
std::string::size_type pos = path.find_last_of("\\/");
return path.substr( 0, pos);
}
Just use the templete CA2CAEX or CA2AEX which calls the internal API ::MultiByteToWideChar or ::WideCharToMultiByte。
if you don't want to use std, you can use this code:
char * ExePath()
{
static char buffer[MAX_PATH] = { 0 };
GetModuleFileName( NULL, buffer, MAX_PATH );
char * LastSlash = strrchr(buffer, '\\');
if(LastSlash == NULL)
LastSlash = strrchr(buffer, '/');
buffer[LastSlash-buffer] = 0;
return buffer;
}
I simply use getcwd() method for that purpose in Windows, and it works pretty well. The code portion is like following:
char cwd[256];
getcwd(cwd, 256);
string cwd_str = string(cwd);
The <unistd.h> library has to be added though.
To find the directory where your executable is, you can use:
TCHAR szFilePath[_MAX_PATH];
::GetModuleFileName(NULL, szFilePath, _MAX_PATH);
If you are using the Poco library, it's a one liner and it should work on all platforms I think.
Poco::Path::current()
On a give Windows C++ IDE I went crude and it was simple, reliable, but slow:
system( "cd" );
String^ exePath = Application::ExecutablePath;<br>
MessageBox::Show(exePath);
In Windows console, you can use the system command CD (Current Directory):
std::cout << "Current Directory = ";
system("cd"); // to see the current executable directory

Stuck using dirent.h to find all files in subfolders C++

I'm currently stuck using dirent.h extension. The goal is to give the function a path directory to start from. That function would then look through all the subfolders from that directory and find files in it. It all works until there are two folders in the same directory. Then the program stubbornly chooses one folder and ignores the other.
Here goes my mess of a code.
(The issue is commented at the bottom of the code)
#include <iostream>
#include "dirent.h"
#include <windows.h>
DWORD getPathType(const char *path); //checks if path leads to folder or file
const char *dirlist[20]; //contains all directories (not files, only dir's)
int dirAm = 0; //specifies amount of directories
void loadTextures(const char *loaddir) {
dirlist[dirAm] = loaddir; //specifies starting directory
dirAm++;
for(int i = 0; i<20; i++) {
DIR *dir;
struct dirent *ent;
const char *currentdir = dirlist[i]; //stores current directory
dir = opendir(currentdir); //opens current directory
if (dir != NULL) {
std::cout << "[OPENING dir]\t" << currentdir << std::endl;
while ((ent = readdir(dir)) != NULL) {
const char *filename; //stores current file/folder name
char fullDirName[100]; //stores full path name (current dir+file name, for example /images/+image1.png)
DWORD filetype; //checking path type (file/folder)
filename = ent->d_name; //gets current file name
strcpy(fullDirName, currentdir); //concats current directory and file name to get full path, for example /images/image1.png
strcat(fullDirName, filename);
filetype = getPathType(fullDirName); //gets path type
if (filetype == FILE_ATTRIBUTE_DIRECTORY) {
//if its a directory add it to the list of directories, dirlist, the naming process is the same as above
const char *filenameIn;
char fullDirNameIn[100];
filenameIn = ent->d_name;
strcpy(fullDirNameIn, currentdir);
strcat(fullDirNameIn, filenameIn);
strcat(fullDirNameIn, "/");
std::cout << "[FOUND dir]\t" << fullDirNameIn<<std::endl;
dirlist[dirAm] = fullDirNameIn;
dirAm++;
/* Here is the problem! The cout line above finds all
folders in a directory and saves them in the array, but as soon as the new for
loop iteration starts, the values in the dirlist array... change? And I have no
idea what is going on */
} else {
std::cout << "[FOUND file]\t" << fullDirName << std::endl;
}
}
}
}
And here is the getPathType() function. Pretty straight forward, I guess.
DWORD getPathType(const char *path) {
DWORD fileat;
fileat = GetFileAttributesA(path);
return fileat;
}
Finally, here is the console output:
[OPENING dir] img/ <- opens starting dir
[FOUND dir] img/lvl0/ <- finds lvl0, should store it in dirlist
[FOUND dir] img/lvl1/ <- finds lvl1
[OPENING dir] img/lvl1/
[FOUND file] img/lvl1/player2.png
[OPENING dir] img/lvl1/ <- only opens lvl1
[FOUND file] img/lvl1/player2.png
I know this is a very big question, but I would be quite thankful if someone could share ideas on this.
You are ignoring the scope of your char arrays. Effectively you are doing this
const char *dirlist[20];
while (...)
{
char fullDirNameIn[100];
....
dirlist[dirAm] = fullDirNameIn;
}
The problem is that your array is scoped to the body of while loop but you are storing a pointer to that array outside the while loop. After you exit the body of the loop (i.e. when you iterate) then contents of your array become undefined, but you still have a pointer to it.
The solution is easy and this should be a lesson well learned. Don't use pointers, do what experienced programmers do and use std::string instead.
std::string dirlist[20];
while (...)
{
std::string fullDirNameIn;
....
dirlist[dirAm] = fullDirNameIn;
}

opening a file in random named folder using c/c++

I'm trying to code a program where it opens and reads a file automatically. But the problem is the file is stored in a folder which name is unknown. I only know where the folder is located and the file's name. How to get to that file's path in char* ?
Edit: example: d:\files\<random folder>\data.txt
I don't know the name of random folder but I know that it exists in d:\files
Since this is tagged windows, you might as well use the Windows API functions:
FindFirstFile()
FindNextFile()
to enumerate and loop through all the files in a given directory.
To check for a directory, look at dwFileAttributes contained in the WIN32_FIND_DATA structure (filled by the calls to Find...File()). But make sure to skip . and .. directories. If needed, this can be done recursively.
You can check the links for some examples, or see Listing the Files in a Directory.
In case you are using MFC, you can use CFileFind (which is a wrapper around the API functions):
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
TRACE(_T("%s\n"), (LPCTSTR)finder.GetFileName());
}
Just for fun, I implemented this using the new, experimental <filesystem> FS Technical Specification supported by GCC 5.
#include <iostream>
#include <experimental/filesystem>
// for readability
namespace fs = std::experimental::filesystem;
int main(int, char* argv[])
{
if(!argv[1])
{
std::cerr << "require 2 parameters, search directory and filename\n";
return EXIT_FAILURE;
}
fs::path search_dir = argv[1];
if(!fs::is_directory(search_dir))
{
std::cerr << "First parameter must be a directory: " << search_dir << '\n';
return EXIT_FAILURE;
}
if(!argv[2])
{
std::cerr << "Expected filename to search for\n";
return EXIT_FAILURE;
}
// file to search for
fs::path file_name = argv[2];
const fs::directory_iterator dir_end; // directory end sentinel
// used to iterate through each subdirectory of search_dir
fs::directory_iterator dir_iter(search_dir);
for(; dir_iter != dir_end; ++dir_iter)
{
// skip non directories
if(!fs::is_directory(dir_iter->path()))
continue;
// check directory for file
// iterate through files in this subdirectory dir_iter->path()
auto file_iter = fs::directory_iterator(dir_iter->path());
for(; file_iter != dir_end; ++file_iter)
{
// ignore directories and wrong filenames
if(fs::is_directory(file_iter->path())
|| file_iter->path().filename() != file_name)
continue;
// Ok we found it (the first one)
std::cout << "path: " << file_iter->path().string() << '\n';
return EXIT_SUCCESS;
}
}
// Not found
std::cout << file_name << " was not found in " << search_dir.string() << '\n';
return EXIT_FAILURE;
}
The idea is: list the directories under d:\files and try to open
the file in each directory.
There isn't (yet) a standard C++ way of getting all the existing files/directories. A crude but easy way of doing this would be
system("dir d:\\files /b /ad > tmpfile");
This lists all directories (/ad), redirected to a temporary file. Then open the file:
std::ifstream list("tmpfile");
And read it:
std::string dirname;
std::string filename;
while (std::getline(list, dirname))
{
filename = "d:\\files\\" + dirname + "\\data.txt";
if ( ... file exists ... )
break;
}
I call this method crude because it has problems that are hard/impossible to fix:
It overwrites a potentially useful file
It doesn't work if current directory is read-only
It will only work in Windows
It might be possible to use _popen and fgets instead of redirecting to file.

How do you make FindFirstFile open a subdirectory of the path you gave it?

I want to go through all the subdirectories of a directory to find all .snt files. But FindFirstFile and FindNextFile only search through the given directory, not through its subdirectories.
Specifically I'm searching through all the subfolders of the sounds folder in the path F:\Program Files (x86)\Amnesia\sounds\
I tried passing F:\\Program Files (x86)\\Amnesia\\sounds\\*\\*.snt to FindFirstFile but it returns garbage. What's the correct way to do this?
#include <windows.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
void main()
{
char path[100]="F:\\Program Files (x86)\\Amnesia\\sounds\\*\\*.snt";
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(path, &FindFileData);
printf("The first file found is %s\n",FindFileData.cFileName);
getch();
}
Well, 'manual' recursion.
Following there is an idea for recursion. What is missing is file detection based on extension.
void file_search_rec(const char *folder)
{
char path[MAX_PATH] ;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
strcpy(path, folder) ;
strcat(path, "\\*") ;
FindFirstFile(path, &FindFileData) ;
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
char subpath[MAX_PATH] ;
strcpy(subpath, folder) ;
strcat(subpath, FindFileData.cFileName) ;
// here make the recursive call on subpath
file_search_rec(subpath) ;
}
}

how to search the computer for files and folders

i need a way to search the computer for files like Windows Explorer. i want my program to search lets say hard drive c:. i need it to search C:\ for folders and files (just the ones you could see in c:\ then if the user clicks on a file on the list like the folder test (C:\test) it would search test and let the user see what files/folders are in it.
Since you mentioned windows, the most straight forward winapi way to do it is with FindFirstFile and FindNextFile functions.
edit: Here's an example that shows you how to enumerate all files/folders in a directory.
#include <Windows.h>
#include <iostream>
int main()
{
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(L"C:\\*",&file);
if (search_handle)
{
do
{
std::wcout << file.cFileName << std::endl;
}while(FindNextFile(search_handle,&file));
FindClose(search_handle);
}
}
This will be OS dependent. The SO question
How can I get a list of files in a directory using C or C++?
handles this problem well. You can download DIRENT here.
Now that you have this, I'd recommend recursively searching for a file with a DFS/BFS algorithm. You can assume the whole directory structure is a tree where each file is a leaf node and each subdirectory is an internal node.
So all you have to do is,
Get the list of files/folders in a directory with a function such as:
void getFilesFolders(vector<string> & dir_list, const string & folder_name)
If it's a directory, go to 1 with the directory name
If it's a file, terminate if it's the file you're looking for, else move on to the next file.
boost::filesystem can be a cross-platform solution for that (check out for such functions in it).
You can use Directory class members to do this with C# or managed C++. See the following MSDN article:
http://support.microsoft.com/kb/307009
If you wish to use C++ with MFC you can use CFileFind
http://msdn.microsoft.com/en-us/library/f33e1618%28v=VS.80%29.aspx
You'll have to supply your own browse window to present the file system tree.
Or you can use one of the directory/file controls to do both for you.
#include <Windows.h>
#include <iostream>
int FindF(char* pDirectory)
{
char szFindPath[MAX_PATH] = {0};
strcpy(szFindPath, pDirectory);
strcat(szFindPath, "\\*");
WIN32_FIND_DATA file;
HANDLE search_handle=FindFirstFile(szFindPath,&file);
if (search_handle)
{
do
{
if(file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
strcpy(szFindPath, pDirectory);
strcat(szFindPath, "\\");
strcat(szFindPath, file.cFileName);
FindF(szFindPath);
}
std::wcout << file.cFileName << std::endl;
}while(FindNextFile(search_handle,&file));
CloseHandle(search_handle);
}
}
There really is no need to use 3rd party library to accomplish this. This is a short, independent function which lists all files (with their paths) in a directory, including subdiretories' files. std::string folderName has to finish with \, and if you want to list all files on computer, just create a loop in calling function along with GetLogicalDriveStrings (It returns strings with \, so it couldn't be more convenient in this case).
void FindAllFiles(std::string folderName)
{
WIN32_FIND_DATA FileData;
std::string folderNameWithSt = folderName + "*";
HANDLE FirstFile = FindFirstFile(folderNameWithSt.c_str(), &FileData);
if (FirstFile != INVALID_HANDLE_VALUE) {
do {
if (strcmp(FileData.cFileName, ".") != 0 && strcmp(FileData.cFileName, "..") != 0)
{
if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::string NewPath = folderName + FileData.cFileName;
NewPath = NewPath + "\\";
FindAllFiles(NewPath);
}
else
{
std::cout /*<< folderName*/ << FileData.cFileName << std::endl;
}
}
} while(FindNextFile(FirstFile, &FileData));
}
}
This is ASCII version, remember that files and folders can be named in Unicode