轉發—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>  
     

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM