在同一个根目录下,给出一个〖参照路径〗,计算〖当前程序目录〗与〖参照路径〗的相对路径
2008-12-01 00:55
351 查看
给朋友写的时候,顺便贴上来了
开发环境为 vc8 + boost1.36
比如给定参照路径为 "c://temp1//temp2",
程序所在目录为 "c://Documents and Settings//Administrator//桌面//boost练习//pathProcess",
则 〖当前程序目录〗与〖参照路径〗的相对路径: ../../../../temp1/temp2
#include <iostream>
#include <string>
#include <boost/filesystem/operations.hpp>
using namespace std;
using namespace boost;
namespace fs = boost::filesystem;
void main()
{
//假设 c://temp1//temp2 为参照目录
const std::string strConsultPath = "c://temp1//temp2";
fs::path ConsultPath( strConsultPath );
if ( !fs::exists( ConsultPath ) )
{
std::cout << "目录 " << strConsultPath << " 不存在" << std::endl;
std::system("pause");
return ;
}
if ( !fs::is_directory( strConsultPath ) )
{
std::cout << strConsultPath << " 不是目录" << std::endl;
std::system("pause");
return ;
}
std::cout << " 参照目录: " << std::endl;
std::cout << strConsultPath << std::endl;
std::cout << std::endl;
//当前程序的完整路径
std::string strCurPath = boost::filesystem::current_path().directory_string();
//当前程序相对于 根目录 的路径
std::string strRet;
while ( 1 )
{
//当前程序的上一级目录
int nPos = strCurPath.rfind('//');
if ( -1 != nPos )
{
strCurPath = strCurPath.substr( 0, nPos );
// 根目录是 C 盘,所以这里固定写成 "c:"
// 实际开发中,要注意一下,最好注意大小写的区分
if ( strCurPath != "c:" )
{
strRet += "..//";
}
else
{
break;
}
}
}
std::cout << " 当前程序相对于 根目录 的路径: " << std::endl;
std::cout << strRet << std::endl;
std::cout << std::endl;
std::cout << " 当前程序相对于 参照目录 的路径: " << std::endl;
int nTmpPos = strConsultPath.find( "//" );
std::string strTmpPath = strConsultPath.substr( nTmpPos + 1, strConsultPath.length() );
strRet += strTmpPath;
std::cout << strRet << std::endl; // ../../../../temp1/temp2
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::system("pause");
return;
}
开发环境为 vc8 + boost1.36
比如给定参照路径为 "c://temp1//temp2",
程序所在目录为 "c://Documents and Settings//Administrator//桌面//boost练习//pathProcess",
则 〖当前程序目录〗与〖参照路径〗的相对路径: ../../../../temp1/temp2
#include <iostream>
#include <string>
#include <boost/filesystem/operations.hpp>
using namespace std;
using namespace boost;
namespace fs = boost::filesystem;
void main()
{
//假设 c://temp1//temp2 为参照目录
const std::string strConsultPath = "c://temp1//temp2";
fs::path ConsultPath( strConsultPath );
if ( !fs::exists( ConsultPath ) )
{
std::cout << "目录 " << strConsultPath << " 不存在" << std::endl;
std::system("pause");
return ;
}
if ( !fs::is_directory( strConsultPath ) )
{
std::cout << strConsultPath << " 不是目录" << std::endl;
std::system("pause");
return ;
}
std::cout << " 参照目录: " << std::endl;
std::cout << strConsultPath << std::endl;
std::cout << std::endl;
//当前程序的完整路径
std::string strCurPath = boost::filesystem::current_path().directory_string();
//当前程序相对于 根目录 的路径
std::string strRet;
while ( 1 )
{
//当前程序的上一级目录
int nPos = strCurPath.rfind('//');
if ( -1 != nPos )
{
strCurPath = strCurPath.substr( 0, nPos );
// 根目录是 C 盘,所以这里固定写成 "c:"
// 实际开发中,要注意一下,最好注意大小写的区分
if ( strCurPath != "c:" )
{
strRet += "..//";
}
else
{
break;
}
}
}
std::cout << " 当前程序相对于 根目录 的路径: " << std::endl;
std::cout << strRet << std::endl;
std::cout << std::endl;
std::cout << " 当前程序相对于 参照目录 的路径: " << std::endl;
int nTmpPos = strConsultPath.find( "//" );
std::string strTmpPath = strConsultPath.substr( nTmpPos + 1, strConsultPath.length() );
strRet += strTmpPath;
std::cout << strRet << std::endl; // ../../../../temp1/temp2
std::cout << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::system("pause");
return;
}
相关文章推荐
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- File类实例给定一个相对路径字符串那当前目录是从哪里来
- 相对路径,绝对路径,当前路径,程序目录
- 用MFC实现代码获取当前EXE 执行的绝对路径,用于扩展相对路径扩展,以及计算文件大小功能
- linux下获取程序当前目录绝对路径
- 获取当前程序所在路径/目录
- matlab当前目录下的相对路径
- 利用Python编写一个程序 统计当前目录下的每个文件类型的文件数目
- 一个php笔试题:两个文件相对路径的计算
- MFC 获得当前程序目录 当前绝对路径
- 酒店项目一笔记:当前目录相对路径
- 相对路径 绝对路径 根目录 上一目录 当前目录
- 在资源管理器中使鼠标右键增加一个命令,运行cmd,同时使得当前路径为资源管理器当前的目录
- [WinAPI] API 12 [获取程序所在的目录、程序模块路径,获取和设置当前目录]
- Java – 查看程序的当前目录(路径)
- 对于一颗完全二叉树,要求给所有节点加上一个pNext指针,指向同一层的相邻节点;如果当前节点已经是该层的最后一个节点,则将pNext指针指向NULL;给出程序实现,并分析时间复杂度和空间复杂度。
- 编写一个程序,统计当前目录下第个文件类型的文件数,程序实现如下图:
- HTML相对路径 当前目录、上级目录、根目录、下级目录表示法
- linux下获取程序当前目录绝对路径