Save/Open Dialog
-
I was wondering what the easiest way to get only the filename from the FileName property of the Save/Open dialogs in Visual C++? I've created a string variable and set it to SaveDialog1.FileName but that contains the path AND the filename. I know I could go through and find the first \ from the end of the variable and remove anything before it but there has to be an easier, less time consuming, way of doing this. In Delphi there was a property of the Save/Open dialogs that would automatically return only the filename and not the path, but, as far as I can tell, there is not one for Visual C++. Any help and/or ideas would be greatly appreciated! Thanks, Tom Sapp http://www.sappsworld.com -- modified at 23:52 Friday 28th October, 2005
-
I was wondering what the easiest way to get only the filename from the FileName property of the Save/Open dialogs in Visual C++? I've created a string variable and set it to SaveDialog1.FileName but that contains the path AND the filename. I know I could go through and find the first \ from the end of the variable and remove anything before it but there has to be an easier, less time consuming, way of doing this. In Delphi there was a property of the Save/Open dialogs that would automatically return only the filename and not the path, but, as far as I can tell, there is not one for Visual C++. Any help and/or ideas would be greatly appreciated! Thanks, Tom Sapp http://www.sappsworld.com -- modified at 23:52 Friday 28th October, 2005
There may be a better way than the following in VC. I don't know, though, as I don't use VC. The following helper functions have worked for me, and you are welcome to them. Maybe somebody else can give us both that better way. 'wString' is a typedef for std::basic_string, which becomes std::string, so you can just replace 'wString' with 'std::string', and it should work for you. If you find any errors in this, all I can say is that I never claimed to be perfect. David
bool rmw::fileExists(const wString & fileName) {
WIN32_FIND_DATA d;
HANDLE file = FindFirstFile(fileName.c_str(), &d);
bool retVal(true);
if (file == INVALID_HANDLE_VALUE) retVal = false;
FindClose(file);
return retVal;
}wString rmw::getPathFromStr(const wString & str) {
int pos = str.find_last_of(TEXT("\\"));
return wString(str.substr(0, pos));
}wString rmw::extractFileName(const wString & str) {
int pos = str.find_last_of(TEXT("\\"));
return wString(str.substr(pos+1, str.length()));
}wString rmw::extractFileExt(const wString & str) {
int pos = str.find_last_of(TEXT("."));
if (pos == -1) return "";
return wString(str.substr(pos, str.length()));
}wString rmw::trimFileExt(const wString & fileName) {
wString fileExt = rmw::extractFileExt(fileName);
int len = fileExt.length();
return fileName.substr(0, fileName.length()-len);
}wString rmw::extractFileDir(const wString & fileName) {
int pos(fileName.find_last_of(TEXT("\\")));
wString t = fileName.substr(0, fileName.length()-pos);
return t;
}(Gotta love that STL!) Debugging - The high art and magic of cussing errors into 'features'
-
I was wondering what the easiest way to get only the filename from the FileName property of the Save/Open dialogs in Visual C++? I've created a string variable and set it to SaveDialog1.FileName but that contains the path AND the filename. I know I could go through and find the first \ from the end of the variable and remove anything before it but there has to be an easier, less time consuming, way of doing this. In Delphi there was a property of the Save/Open dialogs that would automatically return only the filename and not the path, but, as far as I can tell, there is not one for Visual C++. Any help and/or ideas would be greatly appreciated! Thanks, Tom Sapp http://www.sappsworld.com -- modified at 23:52 Friday 28th October, 2005
char path[_MAX_PATH];
char filename[_MAX_FNAME + _MAX_EXT];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath(path,drive,dir,fname,ext);
_makepath(filename,"","",fname,ext);
Software Zen:
delete this;