首页 > 其他 > 详细

相对路径生成绝对路径

时间:2014-09-09 12:43:58      阅读:109      评论:0      收藏:0      [点我收藏+]
#include "stdafx.h"
#include <stack>
#include <string>
#include <iostream>
using namespace std;

void getWord(char* src,stack<string>&s)
{
	string str=src;
	int index=0;

	while(1)
	{
		index=str.find_first_of("/");
		if(index==0)
		{
			str=str.substr(index+1);
			continue;
		}
		else
		{
			string temp=str.substr(0,index);
			s.push(temp);
			str=str.substr(index+1);
			if(str.empty())
			{
				break;
			}
		}
	}
}

void print(stack<string> s)
{
	string str;
	while(!s.empty())
	{
		string temp=s.top();
		if(temp.find("..")!=-1)
		{
			s.pop();
			s.pop();
		}
		else
		{
			str="/"+temp+str;
			s.pop();
		}
	}

	cout<<str.c_str()<<"/"<<endl;
}

int main()
{
	char src[]="/home/news/../tmp/game/../";

	stack<string> s;
	getWord(src,s);
	print(s);

	 system("pause");
	 return 0;
}

bubuko.com,布布扣

相对路径生成绝对路径

原文:http://blog.csdn.net/cjc211322/article/details/39152423

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!