Using C++ ( in visual studio 2013), how can I write a piece of code that searches the system path for a particular file and returns the file's full path? I only have the base name of the file. By system path, I mean the path environment variable in windows (you can see the path by typing "set path" on a command window).
For example. I like to have something like,
file_name = "my_test.xml"
path = find_file(fine_name);
Thanks in advance
I figured out the answer. Below is a piece of code that does exactly what I was looking for:
#include <iostream>
#include <Shlwapi.h>
wchar_t* get_file_full_path(const wchar_t file_name[]){
wchar_t buf[MAX_PATH];
wcscpy_s(buf, wcslen(file_name) + 1, file_name);
if (PathFindOnPath(buf, NULL)) {
std::wcout << file_name << " found." << std::endl;
std::wcout << buf << std::endl;
}
else {
std::wcout << "Could not find the file " << file_name << " on the system path.";
}
return buf;
}