C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 5th, 2008, 09:05 PM
n0oB n0oB is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 11 n0oB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
General - Get file extension from string

I've been trying and trying forever. This is a really noobish question but how can I get the file extension from a string?

Here is what I have (of course it doesn't work):
Code:
std::string getFileExtention( char* file )
	{
		std::string str = file;
		size_t found = str.std::string::find('.');
		if (found != std::string::npos)
		{
			str = str.substr();
		}
	}


Any help will be greatly appreciated, even if it's guiding me toward the right path

Reply With Quote
  #2  
Old July 5th, 2008, 11:22 PM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 159 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 7
string getFileExtension(char* file){

string str = file;
string extension = "";

for(int i=0; i<str.length; i++){
if(str[i] == '.'){
for(int j = i; i<str.length; j++){
extension += str[j];
}//inner for
return extension;
}//if
}//outer for

return extension; //a null string if cant find extension
}


Something I came up on the fly, not sure if this is what you looking for.

Reply With Quote
  #3  
Old July 6th, 2008, 07:41 AM
n0oB n0oB is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 11 n0oB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
For some reason, I was getting a bunch of errors, so I converted it to all char*s. Will this work?

Code:
char* getFileExtension(char* file)
	{

		char* str = file;
		char* extension = "";

		for( int i = 0; i < sizeof(str); i++)
		{
			if ( str[i] == '.' )
			{
				for(int j = i; i < sizeof(str); j++)
				{
					extension += str[j];
				}
				return extension;
			}
		}

		return extension; //a null string if cant find extension
	}

Reply With Quote
  #4  
Old July 6th, 2008, 10:26 AM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 159 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 7
Code:
#include <iostream>
#include <string>
using namespace std;

string getFileExtension(const string& file);


int main(){

	string f = "test.exe";

	string e = getFileExtension(f);
	
	if(e == "")
		cout << "no file extension in " + f;
	else
		cout << e;
	

return 0;

}

string getFileExtension(const string& file){
	
	string str = file;
	string ext = "";
	
		for(int i=0; i<str.length(); i++){
			if(str[i] == '.'){
				for(int j = i; j<str.length(); j++){
					ext += str[j];
				}
				return ext;
			}
			
		}
	
		return ext;
}

Reply With Quote
  #5  
Old July 6th, 2008, 10:43 AM
n0oB n0oB is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 11 n0oB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
Thank you, but I know how to implement it. I need it to return a char*...

Reply With Quote
  #6  
Old July 6th, 2008, 10:51 AM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 159 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 7
Quote:
Originally Posted by n0oB
Thank you, but I know how to implement it. I need it to return a char*...


Then how come your original function you posted has a return type of string?


anyways... you can just cast it to return a char*

Code:
#include <iostream>
#include <string>
using namespace std;

const char* getFileExtension(const string& file);


int main(){

	string f = "test.exe";

	cout << getFileExtension(f);
	
	
	

return 0;

}

const char* getFileExtension(const string& file){
	
	string str = file;
	string ext = "";
	const char* p;
	
		for(int i=0; i<str.length(); i++){
			if(str[i] == '.'){
				for(int j = i; j<str.length(); j++){
					ext += str[j];
				}
				return p = ext.c_str();
			}
			
		}
	
		return p = ext.c_str();
}

Reply With Quote
  #7  
Old July 6th, 2008, 10:54 AM
n0oB n0oB is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 11 n0oB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
Thank you, you are really a big help! I don't know why I said string, I thought it would make it easier but then situations changed.

Reply With Quote
  #8  
Old July 6th, 2008, 01:03 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 7 h 49 m 32 sec
Reputation Power: 10
I am sorry but the code you posted does not work properly. First of all it returns a pointer to memory which has already been freed when the call returns (my compiled program emits garbage instead of the extension) plus you are not returning the extension per se, test it with test.exe.txt, it should return "txt", right? (I would also omit the '.' myself in the result.)
__________________
There is no such thing as C/C++, you either program C or C++

Last edited by Icon : July 6th, 2008 at 01:13 PM.

Reply With Quote
  #9  
Old July 6th, 2008, 10:17 PM
n0oB n0oB is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 11 n0oB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
I figured.. Darken's code works so it's OK.

Reply With Quote
  #10  
Old July 7th, 2008, 04:45 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Sep 2005
Posts: 1,021 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Weeks 7 h 49 m 32 sec
Reputation Power: 10
"it works" is such a fickle notion when it comes to c++...


Why do you need to return a char*? std::strings are superior to character pointers in almost every other way.

If you do want to go the char* route then ask yourself: do you want a pointer to the file extension within the filename itself? That way the extension pointer will become invalid when the filename is destroyed. Or do you want a copy of the file extension, that way you can use the extension after the filename has been destroyed BUT you are responsible for freeing that memory yourself (you could also supply some memory to the function yourself, but that is similar).

Unless offcourse you are only using statically allocated strings (string literals and such) but that strongly limits the reusablility of your function.

I am still convinced the extension of test.ext1.ext2 is .ext2 and not .ext1.ext2 ... But I may be wrong on this one.

Reply With Quote
  #11  
Old July 7th, 2008, 07:09 AM
darken4life darken4life is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 159 darken4life User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 7
N0ob,
If you would like your code to have some error checking of such cases as "text.ext1.ext2" and would like to have extension output ".ext2" you can start from the end and work your way backwards from a for loop and stop when you reach the first '.' and there would be your extension.

Reply With Quote
  #12  
Old July 7th, 2008, 09:31 AM
n0oB n0oB is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2008
Posts: 11 n0oB User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
Thanks for the information, Icon. I'll keep it in mind and will start using strings instead of character pointers.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Get file extension from string


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap