首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

问个读目录的有关问题

2012-03-09 
问个读目录的问题!Dir--Dir1-------file3.txt-------file4.txt--file1.txt--file2.txt例如:Dir目录下有一

问个读目录的问题!
Dir--Dir1-------file3.txt
                  -------file4.txt
      --file1.txt
      --file2.txt

例如:Dir目录下有一个目录Dir1和两个文件file1.txt、file2.txt,而Dir1目录下还有文件file3.txt、file4.txt在C++中怎么循环读目录,把这些文件都读出来?用什么函数?
我要的是能读出file3.txt、file4.txt、file1.txt、file2.txt来处理。
谢谢!

[解决办法]
用函数 firstdata()
nextdata()即可
[解决办法]
posix标准函数:findfirst/findnext
[解决办法]
遍历目录:
long handle;
struct _finddata_t filestruct;  
char path_search[_MAX_PATH];
handle = _findfirst( "目录 ",&filestruct);
if((handle == -1)) return;
if( ::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '. ' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
}
else
{
if( !stricmp(filestruct.name, szFilename) )
{
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
while(!(_findnext(handle,&filestruct)))
{
  if( ::GetFileAttributes(filestruct.name) &FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '. ')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
}

热点排行