gpt4 book ai didi

c - C 语言的目录列表程序

转载 作者:行者123 更新时间:2023-11-30 19:44:24 24 4
gpt4 key购买 nike

我正在用 C 语言编写一个目录列表程序。这是我到目前为止所拥有的。但是,当我在命令提示符中输入 mydir 时。它不打印任何东西。并且程序不会崩溃。有人可以帮我指出我在这里做错了什么吗?谢谢

#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
WIN32_FIND_DATA found; // Search buffer
HANDLE hFind; // Search handle
BOOL fullpath = FALSE; // Full Path requested
char path[MAX_PATH] = ""; // Specified path
char pattern[MAX_PATH]; // Search pattern
char orgpath[MAX_PATH];
int i;
for (i = 1; i < argc; i++) { // Extract parameters from commandline
if (_stricmp(argv[i], "/?") == 0)
printf("[Usage] mydir [/L] <subdirectory path>\n");
else
if (_stricmp(argv[i], "/L") == 0) fullpath = TRUE;
else strcpy_s(path, MAX_PATH, argv[i]); // Save path specification
}
// Check to see if user-specified path exists
GetCurrentDirectory(MAX_PATH, orgpath);
if ((strcmp(path, "") != 0) && !SetCurrentDirectory(path)) {
printf("[Usage] Invalid <subdirectory path> specified\n");
exit(1);
}
// Obtain Full Path if requested
if (fullpath) GetCurrentDirectory(MAX_PATH, path);
SetCurrentDirectory(orgpath); // Restore original path
// Add \ to end of path, if necessary *********************
if (strcmp(path, "") != 0 && // path specified
path[strlen(path) - 1] != ':' && // Not just drive letter
path[strlen(path) - 1] != '\\') // Doesn't end in \
{
strcat_s(path, MAX_PATH, "\\"); // Add \ at end
}

strcpy_s(pattern, MAX_PATH, path);
strcat_s(pattern, MAX_PATH, "*.*"); // Search for all files
hFind = FindFirstFile(pattern, &found);
if (hFind != INVALID_HANDLE_VALUE) { // Found a first file
do {
if ((strcmp(found.cFileName, ".") != 0) &&
(strcmp(found.cFileName, "..") != 0)) {
printf("%s%s\n", path, found.cFileName);
}
} while (FindNextFile(hFind, &found));
FindClose(hFind);
}
return(0);
}

最佳答案

我知道为什么了。我在我的项目中使用 unicode 字符集并传递 ascii 字符串。我将属性更改为多字节字符集,现在它可以工作了。

关于c - C 语言的目录列表程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28316143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com