Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

Need help with getting filename only from OPENFILENAME

This topic is 3202 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

in notepad when a user saves there text file it puts the title in the caption bar at the top. Iam trying to do the same thing. But I can not get just the file name I am getting the fule path.
BOOL SaveAsFile(HWND hwnd, LPSTR pszFileName)
{
     OPENFILENAME ofn;
  
   ZeroMemory(&ofn, sizeof(ofn));
   pszFileName[0] = 0;
   string buffer2;

   ofn.lStructSize = sizeof(ofn);
   ofn.hwndOwner = hwnd;
   ofn.lpstrFilter = "Map Files (*.lvl)\0*.lvl\0All Files (*.*)\0*.*\0\0";
   ofn.lpstrFile = pszFileName;
   ofn.nMaxFile = MAX_PATH;
   ofn.lpstrDefExt = "lvl";
  
    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR;
    ofn.lpstrInitialDir = "Level"; 
    
      if(GetSaveFileName(&ofn))
         {
           SaveLvlFile(pszFileName);
          //gives me full path not just file name

          SetWindowText(hwnd,pszFileName);
         }
   return TRUE;
}
[\source]


Share this post


Link to post
Share on other sites
Advertisement
Couldn't you just look for the last backslash in the full path, and take everything after it? Something like:


char* fileName = pszFileName + strlen(pszFileName);
assert(*fileName == 0);

while(; fileName != pszFileName && *fileName != '\\'; --fileName);

SetWindowText(hwnd, fileName);

Share this post


Link to post
Share on other sites
Just strip the path. Start at the end of the string and go backwards until you hit a file separator / or \\ or whichever your operating system uses, and strip everything from there on.

Share this post


Link to post
Share on other sites
I could be wrong, but I believe there's a member of that struct that gives you just the filename without the rest of the path, i believe it's lpstrFileTitle.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
I thinck so to but I dont know how to use lpstrFileTitle. Any one have any idea.
Are you serious? What about instead of:
SetWindowText(hwnd,pszFileName);
You use:
SetWindowText(hwnd,ofn.lpstrFileTitle);

Edit: actually, I think you've got to supply your own buffer for lpstrFileTitle, but it's no different to how you have to supply your own buffer for lpstrFile.

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
Quote:
Original post by kingpinzs
I thinck so to but I dont know how to use lpstrFileTitle. Any one have any idea.
Are you serious? What about instead of:
SetWindowText(hwnd,pszFileName);
You use:
SetWindowText(hwnd,ofn.lpstrFileTitle);

Edit: actually, I think you've got to supply your own buffer for lpstrFileTitle, but it's no different to how you have to supply your own buffer for lpstrFile.


Yeah, you have to supply your own buffer, so something like this.

char fileTitle[MAX_PATH];


//later in the file

ofn.lpstrFileTitle = fileTitle;

//later in file

SetWindowText(hWnd,fileTitle);


although, you could use ofn.lpstrFileTitle after the buffer has been set, but that's just ugly and wrong

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!