c - 如何在 C++ 中实现正确的文件夹列表

标签 c winapi

包含“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/

相关文章:

c - 动态调度,C

c++ - 由于多个 'main',STM8上的cpputest失败

c - 将 WINAPI 函数指针存储在结构体中

windows - 防止 WritePrivateProfileString 进入注册表

CryptGenRandom 返回不统一的结果

c - 打印链表?

objective-c - 有没有办法在 c 中的另一个#define 中执行#define?

c++ - 在主对话框中嵌入对话框并在 MFC 中单击按钮切换它们

c++ - 在屏幕上定位程序位置

c++ - 以编程方式设置子进程的各种限制 (Linux)