标准C++中有没有处理路径字符串的函数,该如何解决
标准C++中有没有处理路径字符串的函数
比如现在有一个字符串,存的路径
c:\abcde\fghij\klmn\opq.rst
有没有函数可以进行如下操作:
1. 取文件名 opq.rst
2. 取扩展名 .rst或rst
3. 取父目录名 C:\abcde\fghij\klmn\
另,已知目录c:\abcde\fghij\和相对路径klmn\opq.rst
有没有函数可以合并成一个完整路径c:\abcde\fghij\klmn\opq.rst
这些函数就像是在FileSystemObject (FSO)中的
GetFileName
GetExtensionName
GetParentFolderName
和
BuildPath
注意:我是问有没有现成的函数,而不是要自己写。
要自己写到是简单。
------解决方案--------------------
MFC里CFile类里都有啊,
GetPosition() Retrieves the current file pointer.
GetStatus() Retrieves the status of this open file.
GetFileName() Retrieves the filename of the selected file.
GetFileTitle() Retrieves the title of the selected file.
GetFilePath() Retrieves the full file path of the selected file.
SetFilePath() Sets the full file path of the selected file.
你可以上MSDN查一下 上面这些函数都是CFile的成员函数
------解决方案--------------------
不能这样吗?
string s= "c:\\abcde\\fghij\\klmn\\opq.rst ";
1. 取文件名 opq.rst
string file_name=s.substr(s.rfind( '\\ ')+1);
2. 取扩展名 .rst或rst
string ext_name=s.substr(s.rfind( '. '));
3. 取父目录名 C:\abcde\fghij\klmn\
string par_name=s.substr(0,s.rfind( '\\ ')+1);
4.另,已知目录c:\abcde\fghij\和相对路径klmn\opq.rst
有没有函数可以合并成一个完整路径c:\abcde\fghij\klmn\opq.rst
string st3= "c:\\abcde\\fghij\\ ";
string st4= "klmn\\opq.rst ";
string _path=st3+st4;
------解决方案--------------------
_splitpath
微软的 <stdlib.h> 里有啊。
------解决方案--------------------
与之相对的是:_makepath,可以满足你的第二个要求。
------解决方案--------------------
void _splitpath(
const char *path,
char *drive,
char *dir,
char *fname,
char *ext
);
比如现在有一个字符串,存的路径
c:\abcde\fghij\klmn\opq.rst
有没有函数可以进行如下操作:
1. 取文件名 opq.rst
2. 取扩展名 .rst或rst
3. 取父目录名 C:\abcde\fghij\klmn\
另,已知目录c:\abcde\fghij\和相对路径klmn\opq.rst
有没有函数可以合并成一个完整路径c:\abcde\fghij\klmn\opq.rst
这些函数就像是在FileSystemObject (FSO)中的
GetFileName
GetExtensionName
GetParentFolderName
和
BuildPath
注意:我是问有没有现成的函数,而不是要自己写。
要自己写到是简单。
------解决方案--------------------
MFC里CFile类里都有啊,
GetPosition() Retrieves the current file pointer.
GetStatus() Retrieves the status of this open file.
GetFileName() Retrieves the filename of the selected file.
GetFileTitle() Retrieves the title of the selected file.
GetFilePath() Retrieves the full file path of the selected file.
SetFilePath() Sets the full file path of the selected file.
你可以上MSDN查一下 上面这些函数都是CFile的成员函数
------解决方案--------------------
不能这样吗?
string s= "c:\\abcde\\fghij\\klmn\\opq.rst ";
1. 取文件名 opq.rst
string file_name=s.substr(s.rfind( '\\ ')+1);
2. 取扩展名 .rst或rst
string ext_name=s.substr(s.rfind( '. '));
3. 取父目录名 C:\abcde\fghij\klmn\
string par_name=s.substr(0,s.rfind( '\\ ')+1);
4.另,已知目录c:\abcde\fghij\和相对路径klmn\opq.rst
有没有函数可以合并成一个完整路径c:\abcde\fghij\klmn\opq.rst
string st3= "c:\\abcde\\fghij\\ ";
string st4= "klmn\\opq.rst ";
string _path=st3+st4;
------解决方案--------------------
_splitpath
微软的 <stdlib.h> 里有啊。
------解决方案--------------------
与之相对的是:_makepath,可以满足你的第二个要求。
------解决方案--------------------
void _splitpath(
const char *path,
char *drive,
char *dir,
char *fname,
char *ext
);