包含“stdafx.h”
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
void _tmain(int argc, TCHAR *argv[])
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
printf ("Target file is %s.\n", argv[1]);
hFind = FindFirstFile(argv[1], &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFile failed (%d)\n", GetLastError());
system("pause");
return;
}
else
{
do
{
printf("%s\n",FindFileData.cFileName);
}
while (FindNextFile(hFind,&FindFileData)!=0);
FindClose(hFind);
}
system("pause");
FindClose(hFind);
}
我需要在输出中获取一个文件夹列表,但它给了我以下内容:
.
.
f
f
f
实际上,我的文件夹列表是:
.
..
file1
file2
file3
为什么我只有文件名的第一个字母? 谢谢。
最佳答案
使用 _tprintf(TEXT("%s\n"), FindFileData.cFileName)
。
在您的情况下,FindFileData.cFileName
是实际类型 wchar_t
,因此对于 printf
,您将打印宽字符串,就像它是 ascii 一样.
关于c - 如何在 C++ 中实现正确的文件夹列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3352873/