LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2007, 04:54 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
getting a folder name from a string in C


Hi,
can anyone advise is there a simple way to get a folder(s) name from a string passed to a function, in C.
For example I might have loadtext(const char *filename) where file name is Desktop/abc/alpla.txt and I need to store the folders so I'm left with a string Desktop/abc. So basically I strip the final file out of the name I pass. Hope this makes sense and thanks in advance.
 
Old 04-04-2007, 06:15 AM   #2
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
You could try using Perl Compatible Regular Expressions in C.

They are kind of a pain to learn but once you get the hang of it they are not too bad and VERY powerful.

Hope this helps!
 
Old 04-04-2007, 08:06 AM   #3
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
If you just want to locate the last "/", strrchr(3) will do the trick. If you replace the pointer returned with a null, it will crop the string to just be the directory name. Be careful if "/something" is passed though!

HTH,
-nick
 
Old 04-04-2007, 09:47 AM   #4
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
Thank Chaps! I went with the string function, perl seemed a bit of a long way round though I'm sure educational
I used

int s1;
char *p,*name;
int loop=0;

char *n="text.txt"; /*test*/

p=strrchr(filename,'/'); /*get final / put error here for null*/
s1=strlen((filename)-strlen(p))+1;
name=malloc(s1);
memcpy (name, filename, s1);

printf(" %d\n",s1); /*view results*/
printf(" %s \n",name);

strcat(name,n);
printf(" %s \n",name);

Wouldn't you think C would have a function for this?
 
Old 04-11-2007, 07:04 AM   #5
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
There are the basename and dirname functions, which do the same sort of thing, but you still have to concatenate and copy the strings you want.

http://www.die.net/doc/linux/man/man3/dirname.3.html

HTH,
-nick
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
String extracting / string operation Xeratul Linux - General 24 02-13-2007 02:54 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM
Folder Max Size and Limiting SSH access to home folder. Mefistofeles Linux - General 4 11-26-2005 02:09 PM
how can i default the max folder file size when it create inside a folder antony_csf Linux - Software 1 06-17-2004 02:26 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration