Public Group

# Need help with getting filename only from OPENFILENAME

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

## 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 on other sites
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 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 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 on other sites
I thinck so to but I dont know how to use lpstrFileTitle. Any one have any idea.

##### Share on other sites
Quote:
 Original post by kingpinzsI 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 on other sites
Quote:
Original post by Codeka
Quote:
 Original post by kingpinzsI 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 on other sites
I tryed that and it just comes back empty

##### Share on other sites
Did you also set nMaxFileTitle?

##### Share on other sites
ya I added it in and still nothing

1. 1
2. 2
3. 3
Rutin
25
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013119
×