如何向const wchar_t*追加文件名(基于STM32CubeProgrammer API)
自动枚举.hex文件并生成const wchar_t*路径的实现方案
核心思路
- 复用原有的平台分支逻辑确定目标目录前缀
- 遍历目标目录,筛选出所有
.hex后缀的文件并收集完整路径 - 打印文件列表,引导用户输入编号完成选择
- 将选中的路径存入静态/全局缓冲区,确保返回的
const wchar_t*类型路径生命周期满足后续API调用需求
跨平台代码实现
1. 目录遍历与文件筛选函数
实现跨平台的.hex文件枚举逻辑,返回完整路径列表:
#include <vector> #include <string> #include <cwchar> #ifdef _WIN32 #include <windows.h> #else #include <dirent.h> #include <sys/stat.h> #include <cstring> #endif // 枚举指定目录下的所有.hex文件,返回完整路径列表 std::vector<std::wstring> enumerateHexFiles(const wchar_t* targetDir) { std::vector<std::wstring> hexFiles; #ifdef _WIN32 WIN32_FIND_DATAW findData; wchar_t searchPath[MAX_PATH]; swprintf_s(searchPath, MAX_PATH, L"%s\\*.hex", targetDir); HANDLE hFind = FindFirstFileW(searchPath, &findData); if (hFind == INVALID_HANDLE_VALUE) return hexFiles; do { // 跳过目录,仅处理文件 if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { wchar_t fullPath[MAX_PATH]; swprintf_s(fullPath, MAX_PATH, L"%s\\%s", targetDir, findData.cFileName); hexFiles.emplace_back(fullPath); } } while (FindNextFileW(hFind, &findData)); FindClose(hFind); #else // 转换宽字符目录为窄字符(类Unix系统默认用窄字符路径) const char* dirStr = reinterpret_cast<const char*>(targetDir); DIR* dir = opendir(dirStr); if (!dir) return hexFiles; struct dirent* entry; while ((entry = readdir(dir)) != nullptr) { // 跳过目录和非.hex文件 if (entry->d_type != DT_REG) continue; size_t fileNameLen = strlen(entry->d_name); if (fileNameLen < 4 || strcmp(entry->d_name + fileNameLen - 4, ".hex") != 0) continue; // 拼接完整路径并转为宽字符 std::string fullPathStr = std::string(dirStr) + "/" + entry->d_name; hexFiles.emplace_back(fullPathStr.begin(), fullPathStr.end()); } closedir(dir); #endif return hexFiles; }
2. 用户交互与路径生成
替换原有的硬编码路径逻辑,实现用户选择功能:
/* Download File + verification */ const wchar_t* filePath = nullptr; #ifdef _WIN32 const wchar_t* targetDir = L"../test file"; #else const wchar_t* targetDir = L"../api/test file"; #endif // 枚举目标目录下的.hex文件 auto hexFiles = enumerateHexFiles(targetDir); if (hexFiles.empty()) { wprintf(L"No .hex files found in directory: %s\n", targetDir); // 无文件时使用默认路径,可根据需求调整 filePath = L"../test file/default.hex"; } else { // 打印可选文件列表 wprintf(L"Available .hex files:\n"); for (size_t i = 0; i < hexFiles.size(); ++i) { wprintf(L"%zu: %s\n", i + 1, hexFiles[i].c_str()); } // 引导用户输入编号,直到输入合法 int selection = 0; do { wprintf(L"Enter file number (1-%zu): ", hexFiles.size()); wscanf(L"%d", &selection); } while (selection < 1 || selection > static_cast<int>(hexFiles.size())); // 用静态缓冲区存储选中路径,保证const wchar_t*的生命周期 static wchar_t selectedPath[MAX_PATH]; wcscpy_s(selectedPath, MAX_PATH, hexFiles[selection - 1].c_str()); filePath = selectedPath; } // filePath已准备好,可直接传入STM32CubeProgrammer API wprintf(L"Selected file path: %s\n", filePath);
关键说明
- 路径生命周期:使用
static缓冲区存储选中路径,避免局部变量销毁导致的野指针问题,完全符合const wchar_t*的只读要求。 - 跨平台兼容:通过平台宏分支处理Windows和类Unix系统的目录遍历API,逻辑统一且无需额外依赖。
- 用户体验:仅需输入数字编号即可选择文件,避免手动输入路径的繁琐和错误。
- 容错处理:针对无
.hex文件的场景提供默认路径,可根据实际需求扩展错误处理逻辑。
内容的提问来源于stack exchange,提问作者RTL
相关产品推荐
相关产品推荐

