转发—c++实现查找文件夹下的文件

[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
开始以为是个很简单问题后来才发现有点麻烦,最后实现如下,其中tmp是要查找的文件的类型,改成其他的如.java就可以查找java类型文件名  
[cpp] view plain copy 在CODE上查看代码片派生到我的代码片
<pre code_snippet_id="266604" snippet_file_name="blog_20140331_2_3829526" name="code" class="cpp">#include<iostream>  
#include<dirent.h>  
using namespace std;  
int main( void )  
{  
    DIR* dirp;  
    struct dirent* direntp;  
    dirp = opendir( "F:\output\codegen" );  
    string tmp =".txt";  
    string filename;  
    if( dirp != NULL )   
    {  
        for(;;) {  
            direntp = readdir( dirp );  
            if( direntp == NULL )   
        break;  
            string s = direntp->d_name;  
            int x = s.find(tmp,0);             
            if(x>0){  
                string last;  
        last.assign(s,0,x);  
        filename=last;  
        //filename 就是txt文件的名称,不包含txt后缀,得到名称后添加.txt后缀就可以直接打开进行其他操作  
            }  
        }  
        closedir( dirp );  
    }  
    cout<<filename<<endl;  
    return 0;  
}</pre><br>  
<br>  
<pre></pre>  
<pre></pre>  
     

  

原文地址:https://www.cnblogs.com/1996313xjf/p/5907563.html