LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-18-2010, 01:24 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - How to put a specific arbitrary part of a string into it's own string?


So if I'm given a location of a file like:
/something/somethingelse/thefile.html or
/different/moredifferent/otherfile.jpg
How can I just take the type of the file at the end? I know I can use strrchr() for a period to get the pointer to the period just before file type. Is there a build in string function that will just take the rest of the string from a certain point on forward in the string? I know it wouldn't be much work to make it myself, but I figured I would find out if it already existed before doing it.

Thanks!
 
Old 04-18-2010, 01:51 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
too simpe for the special function
Code:
char str[]="/something/somethingelse/thefile.html";
char str_ext[10];
strcpy(str_ext,((char*)strrchr(str,'.'))+1);
 
Old 04-18-2010, 02:26 PM   #3
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Great great, thanks thanks.
 
Old 04-18-2010, 02:32 PM   #4
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
and how well will that scheme work if char str[]="/some.thing/some.thing.else/thefile.html"; ?

Best way it to use pointers and scan backward through the string to find the last period in it.
 
Old 04-18-2010, 02:56 PM   #5
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
Quote:
Originally Posted by jiml8 View Post
and how well will that scheme work if char str[]="/some.thing/some.thing.else/thefile.html"; ?
that scheme work very well. output: html
Quote:
Best way it to use pointers and scan backward through the string to find the last period in it.
strrchr!=strchr

Last edited by troop; 04-18-2010 at 02:58 PM.
 
Old 04-18-2010, 06:36 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
Oops. I missed the extra "r" in the middle of the function.
 
Old 04-18-2010, 07:28 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
OP - if you have your solution, please mark query as SOLVED.
 
Old 04-19-2010, 10:12 AM   #8
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Oops, sorry about not marking solved. I usually remember that.
 
Old 04-19-2010, 11:05 AM   #9
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
OP - if you have your solution, please mark query as SOLVED.
Is it solved?

"/foo/.baz"
"./foo"
"/foo.causeBufferOverFlow"

Marking threads as solved is wrong, especially since the person who marks it as such does not have the knowledge to know if there are problems. I will purposely leave this as an exercise.

Last edited by dmail; 04-19-2010 at 11:09 AM.
 
Old 04-19-2010, 08:27 PM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Whilst i understand your point dmail, I also cannot tell if the OP understands that the solution is not final but may need
some more work just as it may be all they need to solve this particular issue.

But as OPs last post was:
Quote:
Great great, thanks thanks.
I just took it that they probably were not going to revisit this query as they seemed to be satisfied.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Remove files that contain a specific string poymode Linux - General 5 02-17-2010 03:01 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
Extract part of a string steven.c.banks Linux - General 7 05-07-2008 07:18 AM
C: Extracting part of a string trevorv Programming 3 08-29-2007 04:36 PM
How to find those files having specific string??? saeed Red Hat 4 07-19-2006 11:50 AM


All times are GMT -5. The time now is 10:13 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration