Home > Archive > VC Language > November 2005 > How to get filename from path, with or without _splitpath?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to get filename from path, with or without _splitpath?
|
|
|
| Hi all.
I need to get filename and extension from a path. _splitpath seems to
be good for that, you would think that MS knows the rules of paths and
filenames. But MSDN warns about buffer over runs in splitpath
documentation.
Should I check for maximum pathlenght or what?
So is there any rules?
Or do I just search for last "\" for filename and last "." for
extension?
| |
|
|
|
|
| Jochen Kalmbach [MVP] 2005-11-15, 4:02 am |
| Hi Alan!
> For those users not using .NET, where can we find the source code for
> CPathT? I too would like to rid my code of _splitpath.
In .NET you should use System::IO:Path...
And the source for "CPath::FindFileName" is simple:
::PathFindFileName
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
| |
|
| Hi all
"Shell Path Handling Functions" in address
http://msdn.microsoft.com/library/d...i/path/path.asp
Seems to be usefull. But they need still one more include file and
they dont mention Windows XP. I'll try anyway.
Regards
Leif
Jochen Kalmbach [MVP] wrote:
> Hi Alan!
>
>
> In .NET you should use System::IO:Path...
>
>
> And the source for "CPath::FindFileName" is simple:
>
> ::PathFindFileName
>
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
| |
| Ale Contenti [MSFT] 2005-11-15, 7:05 pm |
| You can also use _splitpath_s to protect your code from buffer overruns, with a minimal change to your source code (if you're already using _splitpath).
Let me know if you are unclear on how to use _splitpath_s. Also, the MSDN topic which warns about _splitpath buffer overruns should point you to _splitpath_s.
Thanks!
Ale Contenti
VC++ Libraries Team
-----Original Message-----
From: Alan Carre
Posted At: Monday, November 14, 2005 7:07 PM
Posted To: microsoft.public.vc.language
Conversation: How to get filename from path, with or without _splitpath?
Subject: Re: How to get filename from path, with or without _splitpath?
For those users not using .NET, where can we find the source code for
CPathT? I too would like to rid my code of _splitpath.
- Alan
"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in message
news:OxOtg1V6FHA.3136@TK2MSFTNGP09.phx.gbl...
> Hi LM!
>
>
> MFC: CPath
> http://msdn.microsoft.com/library/e.../vclrfCPath.asp
>
> Win32-API: PathFindFileName
> http://msdn.microsoft.com/library/e...ndextension.asp
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
| |
| Mark Randall 2005-11-15, 7:05 pm |
| One has to wonder why the chaps over in the compiler division havent been
clever and added an option to convert to the _s versions if the size is
known.
--
- Mark Randall
http://zetech.swehli.com
"Those people that think they know everything are a great annoyance to those
of us who do"
Isaac Asimov
"Ale Contenti [MSFT]" <alecont@online.microsoft.com> wrote in message
news:uphg9jg6FHA.2552@TK2MSFTNGP10.phx.gbl...[color=darkred]
> You can also use _splitpath_s to protect your code from buffer overruns,
> with a minimal change to your source code (if you're already using
> _splitpath).
>
> Let me know if you are unclear on how to use _splitpath_s. Also, the MSDN
> topic which warns about _splitpath buffer overruns should point you to
> _splitpath_s.
>
> Thanks!
>
> Ale Contenti
> VC++ Libraries Team
>
> -----Original Message-----
> From: Alan Carre
> Posted At: Monday, November 14, 2005 7:07 PM
> Posted To: microsoft.public.vc.language
> Conversation: How to get filename from path, with or without _splitpath?
> Subject: Re: How to get filename from path, with or without _splitpath?
>
>
> For those users not using .NET, where can we find the source code for
> CPathT? I too would like to rid my code of _splitpath.
>
> - Alan
>
> "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in
> message
> news:OxOtg1V6FHA.3136@TK2MSFTNGP09.phx.gbl...
| |
| Alan Carre 2005-11-16, 4:01 am |
| This is what I get under VC6:
error C2065: '_splitpath_s' : undeclared identifier
Plus I don't see it in the help anywhere. I am not using .NET, I am using
VC6.
- Alan
"Ale Contenti [MSFT]" <alecont@online.microsoft.com> wrote in message
news:uphg9jg6FHA.2552@TK2MSFTNGP10.phx.gbl...[color=darkred]
> You can also use _splitpath_s to protect your code from buffer overruns,
> with a minimal change to your source code (if you're already using
> _splitpath).
>
> Let me know if you are unclear on how to use _splitpath_s. Also, the MSDN
> topic which warns about _splitpath buffer overruns should point you to
> _splitpath_s.
>
> Thanks!
>
> Ale Contenti
> VC++ Libraries Team
>
> -----Original Message-----
> From: Alan Carre
> Posted At: Monday, November 14, 2005 7:07 PM
> Posted To: microsoft.public.vc.language
> Conversation: How to get filename from path, with or without _splitpath?
> Subject: Re: How to get filename from path, with or without _splitpath?
>
>
> For those users not using .NET, where can we find the source code for
> CPathT? I too would like to rid my code of _splitpath.
>
> - Alan
>
> "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in
> message
> news:OxOtg1V6FHA.3136@TK2MSFTNGP09.phx.gbl...
| |
| Alan Carre 2005-11-16, 4:01 am |
| Here is what I get under VC6:
error C2065: 'PathFindFileName' : undeclared identifier
I am not using .NET, I am using VC6.
Thanks,
- Alan
"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in message
news:%23gRzcRb6FHA.1036@tk2msftngp13.phx.gbl...
> Hi Alan!
>
>
> In .NET you should use System::IO:Path...
>
>
> And the source for "CPath::FindFileName" is simple:
>
> ::PathFindFileName
>
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
| |
| Mark Randall 2005-11-16, 4:01 am |
| Ensure you have downloaded the latest platform SDK.
--
- Mark Randall
http://zetech.swehli.com
"Those people that think they know everything are a great annoyance to those
of us who do"
Isaac Asimov
"Alan Carre" <alan@twilightgames.com> wrote in message
news:%23eJhign6FHA.2576@TK2MSFTNGP09.phx.gbl...
> Here is what I get under VC6:
>
> error C2065: 'PathFindFileName' : undeclared identifier
>
> I am not using .NET, I am using VC6.
>
> Thanks,
> - Alan
>
>
> "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in
> message news:%23gRzcRb6FHA.1036@tk2msftngp13.phx.gbl...
>
>
| |
|
|
| Alan Carre 2005-11-16, 8:01 am |
| Good christ. 1.2 Gig of data just to split a path?
I thought someone might just have some code handy that works. I think I'll
stick with _splitpath and allocate 1.2Gig for my path buffer instead.
- Alan
"Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in message
news:enRF6rp6FHA.4012@TK2MSFTNGP14.phx.gbl...
> Hi Mark!
>
>
> Hopefully not the latest, but the latest that works with VC6:
> http://www.microsoft.com/msdownload...e/psdk-full.htm
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
| |
| Jochen Kalmbach [MVP] 2005-11-16, 8:01 am |
| Hi Alan!
> I thought someone might just have some code handy that works. I think I'll
> stick with _splitpath and allocate 1.2Gig for my path buffer instead.
For many different examples, you can simply look at the source of the
MFC/ATL... there are at least 5 different ways to determine the filename
of the EXE/DLL... just search for "GetModuleFileName"...
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
| |
| Eberhard Schefold 2005-11-16, 8:01 am |
| Alan Carre wrote:
> Good christ. 1.2 Gig of data just to split a path?
I guess we all have different working habits, but chances are you will
find other interesting things in an SDK that's more current than the one
that came with VC6. Seven years in software development seems like a lot
to me.
That said, I believe "PathFindFileName" is so old that it was included
even then. Make sure you have the compile guards (here: _WIN32_IE) set
correctly for the required target platform.
| |
| Tom Serface 2005-11-16, 7:05 pm |
| You need to include the SHLWAPI.H file. I think this is done for you in C++
7.1, but not in 6.0.
#include "Shlwapi.h"
I use these functions all the time and they are great... They are part of
Internet Explorer 6 so you'd have to have that version at least.
http://msdn.microsoft.com/library/d...de/versions.asp
You will have to download the SDK, but it certainly won't increase the size
of your program by much to use these functions.
http://www.microsoft.com/downloads/...&displaylang=en
Tom
"Alan Carre" <alan@twilightgames.com> wrote in message
news:%23eJhign6FHA.2576@TK2MSFTNGP09.phx.gbl...
> Here is what I get under VC6:
>
> error C2065: 'PathFindFileName' : undeclared identifier
>
> I am not using .NET, I am using VC6.
>
> Thanks,
> - Alan
>
| |
| Ale Contenti [MSFT] 2005-11-16, 7:05 pm |
| Right. PathFindFileName is in the SDK:
http://msdn.microsoft.com/library/d...indfilename.asp
_splitpath_s is available with Visual Studio 2005:
http://msdn2.microsoft.com/en-us/library/8e46eyt7.aspx
Thx!
Ale Contenti
VC++ Libraries
-----Original Message-----
From: Mark Randall
Posted At: Wednesday, November 16, 2005 1:19 AM
Posted To: microsoft.public.vc.language
Conversation: How to get filename from path, with or without _splitpath?
Subject: Re: How to get filename from path, with or without _splitpath?
Ensure you have downloaded the latest platform SDK.
--
- Mark Randall
http://zetech.swehli.com
"Those people that think they know everything are a great annoyance to those
of us who do"
Isaac Asimov
"Alan Carre" <alan@twilightgames.com> wrote in message
news:%23eJhign6FHA.2576@TK2MSFTNGP09.phx.gbl...
> Here is what I get under VC6:
>
> error C2065: 'PathFindFileName' : undeclared identifier
>
> I am not using .NET, I am using VC6.
>
> Thanks,
> - Alan
>
>
> "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in
> message news:%23gRzcRb6FHA.1036@tk2msftngp13.phx.gbl...
>
>
| |
| Ale Contenti [MSFT] 2005-11-16, 7:05 pm |
| You're right, we did not add the C++ overload as we did for other functions (like for example strcpy): see http://msdn2.microsoft.com/en-us/library/ms175759.aspx for details.
If you compile with VS2005 this test program:
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
#include <stdio.h>
#include <stdlib.h>
int main()
{
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath("c:\temp\file.txt", drive, dir, fname, ext);
}
You still get a warning saying that the use of _splitpath is insecure:
t174.cpp(14) : warning C4996: '_splitpath' was declared deprecated
C:\Program Files (x86)\Microsoft Visual Studio 8\VC\INCLUDE\stdlib.h(744) : see declaration of '_splitpath'
Message: 'This function or variable may be unsafe. Consider using _splitpath_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE. See online help for details.'
If you switch to _splitpath_s, the C++ overloads are on by default (you can see them at http://msdn2.microsoft.com/en-us/library/8e46eyt7), so _splitpath_s will automatically pick up the size of the static buffers and:
_splitpath_s("c:\temp\file.txt", drive, dir, fname, ext);
Compiles ok.
If you happen to have a pointer instead of a static buffer, you'll need to use the version of _splitpath_s() with the explicit buffer size:
_splitpath_s("c:\temp\file.txt", NULL, 0, dir, _countof(dir), fname, _countof(fname), ext, _countof(ext));
Works as well.
I'll mark the request of adding the C++ overloads for _splitpath as a suggestion. Thanks!
Ale Contenti
VC++ Libraries
-----Original Message-----
From: Mark Randall
Posted At: Tuesday, November 15, 2005 10:34 AM
Posted To: microsoft.public.vc.language
Conversation: How to get filename from path, with or without _splitpath?
Subject: Re: How to get filename from path, with or without _splitpath?
One has to wonder why the chaps over in the compiler division havent been
clever and added an option to convert to the _s versions if the size is
known.
--
- Mark Randall
http://zetech.swehli.com
"Those people that think they know everything are a great annoyance to those
of us who do"
Isaac Asimov
"Ale Contenti [MSFT]" <alecont@online.microsoft.com> wrote in message
news:uphg9jg6FHA.2552@TK2MSFTNGP10.phx.gbl...[color=darkred]
> You can also use _splitpath_s to protect your code from buffer overruns,
> with a minimal change to your source code (if you're already using
> _splitpath).
>
> Let me know if you are unclear on how to use _splitpath_s. Also, the MSDN
> topic which warns about _splitpath buffer overruns should point you to
> _splitpath_s.
>
> Thanks!
>
> Ale Contenti
> VC++ Libraries Team
>
> -----Original Message-----
> From: Alan Carre
> Posted At: Monday, November 14, 2005 7:07 PM
> Posted To: microsoft.public.vc.language
> Conversation: How to get filename from path, with or without _splitpath?
> Subject: Re: How to get filename from path, with or without _splitpath?
>
>
> For those users not using .NET, where can we find the source code for
> CPathT? I too would like to rid my code of _splitpath.
>
> - Alan
>
> "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in
> message
> news:OxOtg1V6FHA.3136@TK2MSFTNGP09.phx.gbl...
| |
|
| Hi all.
I found strrchr() function in Stroustrup book. Is it ok to just find
last "/" in path? The filename is to the right from it. Extension is to
the right from first ".". Is there problems doing it this way?
Some comments from my point of view.
There are some OO equivalents to the string functions, but my OO skills
are not so good. And the Stroustrup C++ is not helping there.
_splitpath_s requires Net2005, I have VC6. I have some reasons to keep
using VC6, the reasons may not be valid but still. Also IE is required.
Path function are also good, but they need their include files and they
are windows specific. Of course, my dll is windows specific, too. But
I'll try strrchr() first, if no one tells otherwise.
Regards
Leif M
LM wrote:
> Hi all.
>
> I need to get filename and extension from a path. _splitpath seems to
> be good for that, you would think that MS knows the rules of paths and
> filenames. But MSDN warns about buffer over runs in splitpath
> documentation.
>
> Should I check for maximum pathlenght or what?
>
> So is there any rules?
>
> Or do I just search for last "\" for filename and last "." for
> extension?
| |
| Duane Hebert 2005-11-21, 9:58 pm |
|
"LM" <sala.nimi@mail.com> wrote in message
news:1132594515.109189.256480@g14g2000cwa.googlegroups.com...
> Hi all.
>
> I found strrchr() function in Stroustrup book. Is it ok to just find
> last "/" in path? The filename is to the right from it. Extension is to
> the right from first ".". Is there problems doing it this way?
Well you would need to search for the last \ as well. You may also
want to deal with filenames with multiple '.' or no extension.
If you can use std::string, you can check the find functions.
If you can use boost, you can check boost::filesystem.
| |
| Andy Walldorff 2005-11-22, 8:02 am |
| [snip]
> _splitpath_s requires Net2005, I have VC6. I have some reasons to keep
> using VC6, the reasons may not be valid but still. Also IE is required.
>
I'm not sure where you get that impression. _splitpath is in the CRT. Just
include stdlib.h.
[snip]
|
|
|
|
|