游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4606|回复: 0

boost之3.文件系统

[复制链接]

56

主题

94

帖子

98

积分

注册会员

Rank: 2

积分
98
发表于 2010-8-14 20:48:00 | 显示全部楼层 |阅读模式
这是简单的使用boost文件系统的例子
在devc++下编译需要2个库一个libboostsystem一个libboostfilesystem
基本的研发如下:
#include <cstdlib>
#include <iostream>
#include <boost/filesystem.hpp>
using
namespace std;
namespace fs = boost::filesystem;

int main(int argc, char
*argv[])
{
    std::cout<<"sizeof(intmax_t)is:"<<sizeof(boost::intmax_t)<<'\n';
    fs::path path("main.cpp",fs::native);
    if(!fs::exists(path))
    {  
        return
-1;                     
    }
    if(!fs::is_regular(path))
        return
-1;
    std::cout<<"size is:"<<fs::file_size(path)<<"字节"<<std::endl;   
   
    fs::path full_path(fs::initial_path<fs::path>());
    full_path = fs::system_complete(fs::path("main.cpp"));  
    //! 获取给定文件全路径  
    cout<<"full path name is:"<<full_path.file_string()<<std::endl;
    //! 检测是不是路径
    cout<<"is path:"<<fs::is_directory(full_path)<<std::endl;
  
    unsigned long file_count =
0;
    unsigned long dir_count =
0;
    unsigned long other_count =
0;
    unsigned long err_count =
0;   
    full_path = fs::system_complete(fs::path("C:\\WINDOWS"));
   
    //! 目录迭代
    fs::directory_iterator end_iter;
    for(fs::directory_iterator dir_itr(full_path);dir_itr != end_iter;++dir_itr)
    {
       try
       {
          if(fs::is_directory(dir_itr->status()))
          {
              ++dir_count;
              std::cout<<dir_itr->path().filename()<<"[directory]\n";
          }
          else
if(fs::is_regular_file(dir_itr->status()))
          {
              ++file_count;
              std::cout<<dir_itr->path().filename()<<"\n";
              //! 全路径名字
              std::cout<<dir_itr->path().native_file_string()<<std::endl;
          }
          else
          {
              ++other_count;
              std::cout << dir_itr->path().filename() <<"[other]\n";
          }
       }
       catch(const std::exception & ex)
       {
          ++err_count;
          std::cout<<dir_itr->path().filename()<<"
"<<ex.what()<<std::endl;
       }
    }
    std::cout<<"\n"<<file_count<<"files\n"<<dir_count<<" directories\n"<<other_count<<" others\n"<<err_count<<" errors\n";
   
    //! 生成文件目录
    fs::create_directories(fs::path("filesystem"));
    system(&quotAUSE");
    return EXIT_SUCCESS;
}
//! ccsdu2004

boost文件系统还不错
要是再加入对压缩包或者资源包的支持就更好了!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2024-4-28 12:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表