您的位置:首页 > 其它

相对路径生成绝对路径

2014-09-09 10:08 197 查看
#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;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: