Need help with getting filename only from OPENFILENAME

Started by
10 comments, last by kingpinzs 14 years, 7 months ago
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]


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);

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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.
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.
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
I thinck so to but I dont know how to use lpstrFileTitle. Any one have any idea.
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.
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

--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
I tryed that and it just comes back empty
Did you also set nMaxFileTitle?
ya I added it in and still nothing

This topic is closed to new replies.

Advertisement