Thread: [Dev-C++] ANSI C: How to split path and filename
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <vil...@ya...> - 2001-06-07 09:03:18
|
Hello,
how do I split my path and my filename?
Example:
c:\things\alfa\myfile.txt
like: char *thepath ----> c:\things\alfa
char *thefile ----> myfile.txt
in ANSI C (or C), meaning the same way with DOS or UNIX
There is anywhere to find this kind of things? (I couldn't find in the help
neither in Bob Stout's snippets)
Thanks.
_______________________________________________________________
Do You Yahoo!?
Yahoo! Messenger: Comunicación instantánea gratis con tu gente -
http://messenger.yahoo.es
|
|
From: Fabien V. <fv...@fr...> - 2001-06-07 09:29:08
|
What you can do is a loop, where the condition is " do I find another \ " ? And you could do like this. You find the first "\" copy "c:\" in a string. Look for another "\" paste the sequel to the first string I mean : "things\" to " c:\" and you get a string with : "c:\things\" then you look for another "\" And you have : "alfa\" paste it to your string, and you get "c:\things\alfa\" Since you don't have anymore "\" the rest of the string will be a filename. Use the strstr() function strcp() ( if I remember the name ). They are all in the string.h Hope it helps. Fabien. fv...@fr... ----- Original Message ----- From: "Albert Vilella Bertran" <vil...@ya...> To: "DevC MAILING List" <dev...@li...> Sent: Thursday, June 07, 2001 10:50 AM Subject: [Dev-C++] ANSI C: How to split path and filename > Hello, > how do I split my path and my filename? > Example: > > c:\things\alfa\myfile.txt > > like: char *thepath ----> c:\things\alfa > char *thefile ----> myfile.txt > > in ANSI C (or C), meaning the same way with DOS or UNIX > > There is anywhere to find this kind of things? (I couldn't find in the help > neither in Bob Stout's snippets) > > Thanks. > > _______________________________________________________________ > Do You Yahoo!? > Yahoo! Messenger: Comunicación instantánea gratis con tu gente - > http://messenger.yahoo.es > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Max T. H. <ma...@va...> - 2001-06-07 11:19:23
|
Why wouldn't you go from the end to the beginning, and try to find a '\'? When you do, everything after is a file, and before is the path. Similar, but I'd say a little bit faster. Max ----- Original Message ----- From: "Fabien VIVES" <fv...@fr...> To: <dev...@li...> Sent: Thursday, June 07, 2001 11:29 AM Subject: Re: [Dev-C++] ANSI C: How to split path and filename > What you can do is a loop, where the condition is " do I find another \= " ? > And you could do like this. > You find the first "\" > copy "c:\" in a string. > Look for another "\" > paste the sequel to the first string > I mean : "things\" to " c:\" > and you get a string with : "c:\things\" > then you look for another "\" > And you have : "alfa\" paste it to your string, and you get > "c:\things\alfa\" > Since you don't have anymore "\" the rest of the string will be a filename. > Use the strstr() function > strcp() ( if I remember the name ). > They are all in the string.h > Hope it helps. > > Fabien. > fv...@fr... > > ----- Original Message ----- > From: "Albert Vilella Bertran" <vil...@ya...> > To: "DevC MAILING List" <dev...@li...> > Sent: Thursday, June 07, 2001 10:50 AM > Subject: [Dev-C++] ANSI C: How to split path and filename > > > > Hello, > > how do I split my path and my filename? > > Example: > > > > c:\things\alfa\myfile.txt > > > > like: char *thepath ----> c:\things\alfa > > char *thefile ----> myfile.txt > > > > in ANSI C (or C), meaning the same way with DOS or UNIX > > > > There is anywhere to find this kind of things? (I couldn't find in th= e > help > > neither in Bob Stout's snippets) > > > > Thanks. > > > > _______________________________________________________________ > > Do You Yahoo!? > > Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu gente - > > http://messenger.yahoo.es > > > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Fabien V. <fv...@fr...> - 2001-06-07 15:31:23
|
Sure it would be faster. LOL! = P And for the matter of the "/" (UNIX) and "\" ( DOS ) well, you could perform a first research on the "/" or the "\". If you don't get one then you know which one to use, and you could set a state variable to remember which one to find next. ----- Original Message ----- From: "Max The Hunted" <ma...@va...> To: <dev...@li...> Sent: Thursday, June 07, 2001 1:18 PM Subject: Re: [Dev-C++] ANSI C: How to split path and filename Why wouldn't you go from the end to the beginning, and try to find a '\'? When you do, everything after is a file, and before is the path. Similar, but I'd say a little bit faster. Max ----- Original Message ----- From: "Fabien VIVES" <fv...@fr...> To: <dev...@li...> Sent: Thursday, June 07, 2001 11:29 AM Subject: Re: [Dev-C++] ANSI C: How to split path and filename > What you can do is a loop, where the condition is " do I find another \ " ? > And you could do like this. > You find the first "\" > copy "c:\" in a string. > Look for another "\" > paste the sequel to the first string > I mean : "things\" to " c:\" > and you get a string with : "c:\things\" > then you look for another "\" > And you have : "alfa\" paste it to your string, and you get > "c:\things\alfa\" > Since you don't have anymore "\" the rest of the string will be a filename. > Use the strstr() function > strcp() ( if I remember the name ). > They are all in the string.h > Hope it helps. > > Fabien. > fv...@fr... > > ----- Original Message ----- > From: "Albert Vilella Bertran" <vil...@ya...> > To: "DevC MAILING List" <dev...@li...> > Sent: Thursday, June 07, 2001 10:50 AM > Subject: [Dev-C++] ANSI C: How to split path and filename > > > > Hello, > > how do I split my path and my filename? > > Example: > > > > c:\things\alfa\myfile.txt > > > > like: char *thepath ----> c:\things\alfa > > char *thefile ----> myfile.txt > > > > in ANSI C (or C), meaning the same way with DOS or UNIX > > > > There is anywhere to find this kind of things? (I couldn't find in the > help > > neither in Bob Stout's snippets) > > > > Thanks. > > > > _______________________________________________________________ > > Do You Yahoo!? > > Yahoo! Messenger: Comunicación instantánea gratis con tu gente - > > http://messenger.yahoo.es > > > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > http://lists.sourceforge.net/lists/listinfo/dev-cpp-users > _______________________________________________ Dev-cpp-users mailing list Dev...@li... http://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Ioannis V. <no...@ya...> - 2001-06-07 20:37:04
|
Use something like:
#include <string.h>
/* ... */
char s[]=3D"...";
while(strstr(s, "\\")!=3DNULL)
*strstr(s, "\\")=3D'/';
/* ... */
Ioannis
* Ioannis Vranos
* Programming pages: http://members.nbci.com/noicys
* Alternative URL: http://run.to/noicys
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Albert
Vilella Bertran
Sent: Thursday, June 07, 2001 10:51 AM
To: DevC MAILING List
Subject: [Dev-C++] ANSI C: How to split path and filename
Hello,
how do I split my path and my filename?
Example:
c:\things\alfa\myfile.txt
like: char *thepath ----> c:\things\alfa
char *thefile ----> myfile.txt
in ANSI C (or C), meaning the same way with DOS or UNIX
There is anywhere to find this kind of things? (I couldn't find in the
help neither in Bob Stout's snippets)
Thanks.
_______________________________________________________________
Do You Yahoo!?
Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu gente -
http://messenger.yahoo.es
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
http://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|
|
From: Ioannis V. <no...@ya...> - 2001-06-07 20:48:13
|
The previous code that I sent is for converting a dos/windows path to unix path. I hadn't read carefully what you have written. If you want to split the directory path from the filename in a dos/windows environment use this: #include <string.h> ... char s[]=3D"path\\filename"; char filename[255]; char path[1024]; size_t i; char *p=3Dstrrchr(s, "\\"); strcpy(filename, p); for(i=3D0; i<p-s; i++) path[i]=3Ds[i]; path[i]=3D'\0'; This can be done better but it is a quick (and error-prone) example. Ioannis * Ioannis Vranos * Programming pages: http://members.nbci.com/noicys * Alternative URL: http://run.to/noicys -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Albert Vilella Bertran Sent: Thursday, June 07, 2001 10:51 AM To: DevC MAILING List Subject: [Dev-C++] ANSI C: How to split path and filename Hello, how do I split my path and my filename? Example: c:\things\alfa\myfile.txt like: char *thepath ----> c:\things\alfa char *thefile ----> myfile.txt in ANSI C (or C), meaning the same way with DOS or UNIX There is anywhere to find this kind of things? (I couldn't find in the help neither in Bob Stout's snippets) Thanks. _______________________________________________________________ Do You Yahoo!? Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu gente - http://messenger.yahoo.es _______________________________________________ Dev-cpp-users mailing list Dev...@li... http://lists.sourceforge.net/lists/listinfo/dev-cpp-users |
|
From: Luigi S. <gi...@li...> - 2001-06-08 01:29:13
|
I hope that you'll find the attached files of some help. I suggest that upon splitting the name of the file, the separator is not=20 discarded, instead it is kept as the last char of the path: doing so allows= =20 you to rebuild the name of the file by joining what has been returned by= =20 "thepath" (as long as it is not NULL) and what has been returned by=20 "thefile" regardless of which is the separator (i.e. a backslash, a colon=20 or a slash). At 10.50 07/06/01 +0200, you wrote: >Hello, >how do I split my path and my filename? >Example: > >c:\things\alfa\myfile.txt > >like: char *thepath ----> c:\things\alfa > char *thefile ----> myfile.txt > >in ANSI C (or C), meaning the same way with DOS or UNIX > >There is anywhere to find this kind of things? (I couldn't find in the help >neither in Bob Stout's snippets) > >Thanks. > >_______________________________________________________________ >Do You Yahoo!? >Yahoo! Messenger: Comunicaci=F3n instant=E1nea gratis con tu gente - >http://messenger.yahoo.es > >_______________________________________________ >Dev-cpp-users mailing list >Dev...@li... >http://lists.sourceforge.net/lists/listinfo/dev-cpp-users -- gisan |