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

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

问题描述

我已经搜索了一切,但没有源代码我发现工作与VS C + + 2008,

你有办法找到目录中的文件列表程序吗?



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

解决方案

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

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<<\\\
<< search_data.cFileName;

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

//使用后关闭句柄或内存/资源泄漏
FindClose(handle);
return 0;
}

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

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 programatically?

I am using VS 2008 C++ on Windows.

解决方案

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;
}

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

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

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