我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

文件夹操作

Posted on 2009-03-31 16:03 Hero 阅读(256) 评论(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] ;//临时变量
 28 
 29     ////获取当前执行文件路径
 30     ::GetModuleFileNameA( NULL, tPath, size ) ;//获取当前执行文件路径
 31 
 32     filePath = tPath ;
 33     cout << "filePath = " << filePath << endl ;
 34 
 35     //获取当前工作目录 -- getcwd()
 36     if( NULL == getcwd( tPath, size ) )
 37         perror( "getcwd error !\n" ) ;
 38     else
 39         cout << tPath << endl ;
 40     filePath = tPath ;
 41 
 42     //更改当前工作目录 -- 相对路径方式 -- chdir()
 43     if( chdir( "../HLOJ" ) )
 44         cout << "Unable to locate the directory you specified !" << endl ;
 45     else
 46     {
 47         getcwd( tPath, size ) ;//重新获取当前工作目录
 48         cout  << "The CWD is " << tPath << endl ;//输出当前工作目录 
 49         system( "type HLOJ_1130.cpp" ) ;//system执行DOS指令
 50     }
 51 
 52     //更改当前工作目录 -- 绝对路径方式 -- chdir()
 53     if( chdir( "D:\\wang" ) )
 54         cout << "Unable to locate the directory you specified !" << endl ;
 55     else
 56     {
 57         getcwd( tPath, size ) ;//重新获取当前工作目录
 58         cout << tPath << endl ;
 59         system( "dir *.bat" ) ;//dir D:/wang
 60     }
 61 
 62     //查找当前目录中符合要求的文件,并且输出文件的相关信息 -- 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 ) ;//对文件从小到大排序
 84     for( vector<string>::iterator it=vec.begin(); it!=vec.end(); it++ )
 85     {
 86         cout << *it << endl ;
 87     }
 88     cout << "*****************************************************" << endl ;
 89     
 90     //查找当前目录中符合要求的文件,并且输出文件的相关信息 -- 底层
 91 
 92     _getcwd( tPath, size ) ;
 93     cout << tPath << endl ;
 94 
 95     WIN32_FIND_DATAA findData ;//定义文件结构变量
 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 }

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理