Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Save/Open Dialog

Save/Open Dialog

Scheduled Pinned Locked Moved C / C++ / MFC
c++delphicsscomhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Sapp
    wrote on last edited by
    #1

    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

    R G 2 Replies Last reply
    0
    • T Tom Sapp

      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

      R Offline
      R Offline
      RandomMonkey
      wrote on last edited by
      #2

      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'

      1 Reply Last reply
      0
      • T Tom Sapp

        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

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        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;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups