如何以编程方式获取目录中的文件列表 [英] How to get list of files in a directory programmatically

查看:113
本文介绍了如何以编程方式获取目录中的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了所有内容,但没有找到与VS C ++ 2008兼容的源代码,

您是否可以通过编程方式在目录中查找文件列表?

I have searched everything , but no source codes i found work with VS C++ 2008,
Do you have any way to find list of files in a directory programmatically?

我正在Windows上使用VS 2008 C ++。

I am using VS 2008 C++ on Windows.

推荐答案

这将在 C:驱动器,它不使用 dirent.h 只是简单的文件处理api,

FindFirstFile & FindNextFile

This shall find the list of files in C: drive, It doesn't use dirent.h just simple file handling api's,
FindFirstFile & FindNextFile

#include <windows.h>

int main(int argc, char* argv[])
{
   WIN32_FIND_DATA search_data;

   memset(&search_data, 0, sizeof(WIN32_FIND_DATA));

   HANDLE handle = FindFirstFile("c:\\*", &search_data);

   while(handle != INVALID_HANDLE_VALUE)
   {
      cout<<"\n"<<search_data.cFileName;

      if(FindNextFile(handle, &search_data) == FALSE)
        break;
   }

   //Close the handle after use or memory/resource leak
   FindClose(handle);
   return 0;
}

您应该查看msdn网站上的标准api。

You should have a look at the standard api's on the msdn website.

这篇关于如何以编程方式获取目录中的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆