|
 |
|
Dev Articles Community Forums
> Programming
> C/C++ Help
|
General - Get file extension from string
Discuss Get file extension from string in the C/C++ Help forum on Dev Articles. Get file extension from string C/C++ Help forum discussing building and maintaining applications in C/C++. Find out why these languages are the foundation on which other languages are built.
|
|
 |
|
|
|
|

Dev Articles Community Forums Sponsor:
|
|
|

July 5th, 2008, 09:05 PM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 11
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 
|

July 5th, 2008, 11:22 PM
|
|
Contributing User
|
|
Join Date: Jun 2007
Posts: 159
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 6
|
|
|
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.
|

July 6th, 2008, 07:41 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 11
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
}
|

July 6th, 2008, 10:26 AM
|
|
Contributing User
|
|
Join Date: Jun 2007
Posts: 159
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 6
|
|
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;
}
|

July 6th, 2008, 10:43 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 11
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*...
|

July 6th, 2008, 10:51 AM
|
|
Contributing User
|
|
Join Date: Jun 2007
Posts: 159
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 6
|
|
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();
}
|

July 6th, 2008, 10:54 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 11
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.
|

July 6th, 2008, 01:03 PM
|
 |
Command Line Warrior
|
|
Join Date: Sep 2005
Posts: 1,021

Time spent in forums: 2 Weeks 7 h 49 m 32 sec
Reputation Power: 9
|
|
|
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.
|

July 6th, 2008, 10:17 PM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 11
Time spent in forums: 1 h 21 m 27 sec
Reputation Power: 0
|
|
|
I figured.. Darken's code works so it's OK.
|

July 7th, 2008, 04:45 AM
|
 |
Command Line Warrior
|
|
Join Date: Sep 2005
Posts: 1,021

Time spent in forums: 2 Weeks 7 h 49 m 32 sec
Reputation Power: 9
|
|
|
"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.
|

July 7th, 2008, 07:09 AM
|
|
Contributing User
|
|
Join Date: Jun 2007
Posts: 159
Time spent in forums: 1 Day 5 h 28 m 57 sec
Reputation Power: 6
|
|
|
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.
|

July 7th, 2008, 09:31 AM
|
|
Registered User
|
|
Join Date: Jul 2008
Posts: 11
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|