• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            我希望你是我獨(dú)家記憶

            一段永遠(yuǎn)封存的記憶,隨風(fēng)而去
            posts - 263, comments - 31, trackbacks - 0, articles - 3
               :: 首頁(yè) :: 新隨筆 ::  :: 聚合  :: 管理

            文件夾操作

            Posted on 2009-03-31 16:03 Hero 閱讀(276) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C++
              1 //C++ 文件夾操作
              2 
              3 #include <iostream>
              4 #include <string>
              5 #include <direct.h>//getcwd(); chdir();
              6 #include <windows.h>//GetModuleFileNameA();
              7 #include <io.h>
              8 #include <vector>
              9 #include <algorithm>
             10 
             11 using namespace std ;
             12 
             13 const int size = 100 ;
             14 
             15 bool cmp( const string& a, const string& b )
             16 {
             17     if( a.length() != b.length() )
             18         return a.length() < b.length() ;
             19     else
             20         return strcmp( a.c_str(), b.c_str() ) < 0 ;
             21 }
             22 
             23 int main()
             24 {
             25     string filePath ;//定義文件路徑
             26     string fileName ;//定義文件名字
             27     char tPath[size] ;//臨時(shí)變量
             28 
             29     ////獲取當(dāng)前執(zhí)行文件路徑
             30     ::GetModuleFileNameA( NULL, tPath, size ) ;//獲取當(dāng)前執(zhí)行文件路徑
             31 
             32     filePath = tPath ;
             33     cout << "filePath = " << filePath << endl ;
             34 
             35     //獲取當(dāng)前工作目錄 -- getcwd()
             36     if( NULL == getcwd( tPath, size ) )
             37         perror( "getcwd error !\n" ) ;
             38     else
             39         cout << tPath << endl ;
             40     filePath = tPath ;
             41 
             42     //更改當(dāng)前工作目錄 -- 相對(duì)路徑方式 -- chdir()
             43     if( chdir( "../HLOJ" ) )
             44         cout << "Unable to locate the directory you specified !" << endl ;
             45     else
             46     {
             47         getcwd( tPath, size ) ;//重新獲取當(dāng)前工作目錄
             48         cout  << "The CWD is " << tPath << endl ;//輸出當(dāng)前工作目錄 
             49         system( "type HLOJ_1130.cpp" ) ;//system執(zhí)行DOS指令
             50     }
             51 
             52     //更改當(dāng)前工作目錄 -- 絕對(duì)路徑方式 -- chdir()
             53     if( chdir( "D:\\wang" ) )
             54         cout << "Unable to locate the directory you specified !" << endl ;
             55     else
             56     {
             57         getcwd( tPath, size ) ;//重新獲取當(dāng)前工作目錄
             58         cout << tPath << endl ;
             59         system( "dir *.bat" ) ;//dir D:/wang
             60     }
             61 
             62     //查找當(dāng)前目錄中符合要求的文件,并且輸出文件的相關(guān)信息 -- IO.h
             63     int hFile ;
             64     _finddata_t fileInfo ;
             65         
             66     vector<string> vec ;//用于保存文件名字 -- 可排序用
             67 
             68     vec.clear() ;
             69     if( (hFile = _findfirst( "*.txt"&fileInfo )) != EOF )    
             70     {
             71         do
             72         {
             73             if!(fileInfo.attrib & _A_SUBDIR ) )//檢查是否是目錄
             74             {
             75                 cout << fileInfo.name << "" << fileInfo.size << "Bytes" << endl ; 
             76                 vec.push_back( fileInfo.name ) ;
             77             }
             78         }while0 == _findnext( hFile, &fileInfo ) ) ;
             79 
             80         _findclose( hFile ) ;
             81     }
             82 
             83     sort( vec.begin(), vec.end(), cmp ) ;//對(duì)文件從小到大排序
             84     for( vector<string>::iterator it=vec.begin(); it!=vec.end(); it++ )
             85     {
             86         cout << *it << endl ;
             87     }
             88     cout << "*****************************************************" << endl ;
             89     
             90     //查找當(dāng)前目錄中符合要求的文件,并且輸出文件的相關(guān)信息 -- 底層
             91 
             92     _getcwd( tPath, size ) ;
             93     cout << tPath << endl ;
             94 
             95     WIN32_FIND_DATAA findData ;//定義文件結(jié)構(gòu)變量
             96     ::ZeroMemory( &findData, sizeof(findData) ) ;
             97 
             98     int fileCnt = 0 ;
             99     HANDLE hFind = ::FindFirstFileA( "*.txt"&findData ) ;
            100     //if( hFind != INVALID_HANDLE_VALUE  )//失敗返回INVALID_HANDLE_VALUE
            101     if( hFind != (HANDLE)EOF )
            102     {
            103         do
            104         {
            105             if'.' == findData.cFileName[0] ) continue ;
            106 
            107             long long fileSize = (long long)(findData.nFileSizeHigh<<32)
            108                                            + findData.nFileSizeLow ;
            109             cout << ++fileCnt << "\\" << findData.cFileName << "***" << fileSize << endl ;
            110         }while( FindNextFileA( hFind, &findData ) ) ;
            111 
            112         FindClose( hFind ) ;
            113     }
            114 
            115     return 0 ;
            116 }
            久久AV高清无码| 996久久国产精品线观看| 国产精品成人久久久| 亚洲国产精品成人久久| 久久精品国产亚洲网站| 久久久久人妻一区精品| 欧美国产成人久久精品| 久久久无码一区二区三区| 久久久久国色AV免费观看| 久久人人爽人人爽人人av东京热| 久久久久亚洲AV无码专区体验| AV无码久久久久不卡网站下载| 亚洲欧洲精品成人久久奇米网| 久久精品国产亚洲av水果派| 久久综合一区二区无码| 久久久久亚洲AV成人片| 囯产极品美女高潮无套久久久| 国产精品日韩深夜福利久久| 久久精品www人人爽人人| 久久国产成人午夜AV影院| 99re这里只有精品热久久| 国产成人精品三上悠亚久久| 欧美精品一区二区久久| 久久亚洲国产中v天仙www | 久久精品人成免费| 久久精品国产99久久香蕉| 久久线看观看精品香蕉国产| 无码精品久久久久久人妻中字| 久久久久亚洲精品男人的天堂| 国产一级持黄大片99久久| 亚洲国产高清精品线久久| 精品久久人人爽天天玩人人妻| 久久久久国产精品| 精品久久久久久无码专区| 国产香蕉久久精品综合网| 久久影院午夜理论片无码| 无码人妻少妇久久中文字幕| 久久精品国产亚洲一区二区三区| 天天爽天天狠久久久综合麻豆| 少妇高潮惨叫久久久久久| 人妻无码αv中文字幕久久琪琪布|