希望能给出改进建议,谢谢。
编译环境:DEV-C++4.9.9.0
/*
Name:
Copyright:
Author: ccnupq
Date: 13-08-07 11:15
Description: 从一个给定的完整的文件路径
(如"C:/My Documents/Software Test 1.00.doc")中,析取文件名,扩展名和文件所处目录
*/
#include<iostream>
using namespace std;
void parse(char* filepath)
{
char *filename,*extname,*dir;
int length=strlen(filepath);
int i=length-1;
char *pos;
//////
int count=0;
while(filepath[i] != '.')
{
i--;
count++;
}
pos=&filepath[i+1];
extname=new char[count+1];
strcpy(extname,pos);
cout<<extname<<endl;
///////
count=0;
filepath[i]='/0';
while(filepath[i] != '/')// "/"改成'/'的时候'
//while(filepath[i] != '/')// "/"改成'//'的时候'
{
i--;
count++;
}
pos=&filepath[i+1];
filename=new char[count+1];
strcpy(filename,pos);
cout<<filename<<endl;
////
filepath[i]='/0';
dir=&filepath[0];
cout<<dir<<endl;
}
int main()
{
char FilePathIns[]="C:/My Documents/Software Test 1.00.doc";
//重点关注这个地方
//文件路径的的输入不能写成题目中的"/"形式.因为"/"会被过滤掉,成为转义字符
//输入的时候把/改成'//'或者'/'都可以
parse(FilePathIns);
system("pause");
return 0;
}