我正在用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);
}
答案 0 :(得分:0)
我发现了原因。我在我的项目中使用unicode字符集并传递ascii字符串。我将属性更改为多字节字符集,现在可以正常工作。