I'm new to C++ world, I stuck with a very trivial problem i.e. to get file name without extension.
I have TCHAR
variable containing sample.txt
, and need to extract only sample
, I used PathFindFileName
function it just return same value what I passed.
I tried googling for solution but still no luck?!
EDIT: I always get three letter file extension, I have added the following code,
but at the end I get something like Montage (2)««þîþ
how do I avoid junk chars at the end?
TCHAR* FileHandler::GetFileNameWithoutExtension(TCHAR* fileName)
{
int fileLength = _tcslen(fileName) - 4;
TCHAR* value1 = new TCHAR;
_tcsncpy(value1, fileName, fileLength);
return value1;
}
Try this:
Assuming the file name is in a string.
This will count up the letters in your original file name and add them one by one to the new file name string.
There are several ways to do this, but this is one basic way to do it.
Try below solution,
Here's how it's done.
This returns a std::string or a std::wstring, as appropriate to the UNICODE setting. To get back to a TCHAR*, you need to use StringType::c_str(); This is a const pointer, so you can't modify it, and it is not valid after the string object that produced it is destroyed.
You can use PathRemoveExtension function to remove extension from filename.
To get only the file name (with extension), you may have first to use PathStripPath, followed by
PathRemoveExtension
.