使用boost库获取应用程序的所在目录

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

作者:朱金灿
来源:http://blog.csdn.net/clever101/


         在Windows平台下我们知道可以使用API函数GetModuleFileName来获取应用程序的路径。最近发现boost库也可以实现这个功能,个人感觉用起来更方便。


             用法很简单,具体代码如下:

  1. #include <iostream>  
  2. using std::cout;  
  3. #include <string>  
  4. using std::string;  
  5. #include "boost/algorithm/string.hpp"  
  6. #include "boost/filesystem/path.hpp"  
  7. #include "boost/filesystem/operations.hpp"  
  8. #include "boost/format.hpp"  
  9. int main(int argc, _TCHAR* argv[])  
  10. {  
  11.     string fullpath = boost::filesystem::initial_path<boost::filesystem::path>().string();  
  12.     cout<<fullpath;  
  13.     system("PAUSE");  
  14.     return 0;  
  15. }  
#include <iostream>using std::cout;#include <string>using std::string;#include "boost/algorithm/string.hpp"#include "boost/filesystem/path.hpp"#include "boost/filesystem/operations.hpp"#include "boost/format.hpp"int main(int argc, _TCHAR* argv[]){ string fullpath = boost::filesystem::initial_path<boost::filesystem::path>().string();    cout<<fullpath; system("PAUSE"); return 0;} 


      我是使用VS 2008(带sp1)编译这个程序的。值得注意的是要顺利获取exe的路径,还得在VS 中将工作目录设置为应用程序的输出路径,如下图:


exe path which boost get


   
      否则获取的就不是正确的应用程序路径了(貌似是当前目录路径,不过这个我没有验证)。其它编译器要做何设置有待其它同学研究。


      注意,使用boost库的这种方法获取的是应用程序所在的目录,而使用API函数GetModuleFileName获取的是应用程序的全路径,如boost方法获取的是e:/BoostLearn/outdir/Debug,而API函数GetModuleFileName获取的是e:/BoostLearn/outdir/debug/BoostBind.exe(你可以看到boost获取的路径用的是'/',而GetModuleFileName用的是'/',可以肯定的是这个只是形式不一样,都是正确的路径)。


      在实际中我们常常需要的只是应用程序所在的目录,而使用API函数GetModuleFileName获取的是应用程序的全路径还要对字符串进行分割,所以我说boost库的方法更方便。另外一个不确定的好处是这会不会是一种跨平台的获取应用程序路径的做法。







           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值