Solved

Like to know how to get the file name extenssion

Posted on 2007-12-03
6
217 Views
Last Modified: 2011-09-20
I have a utility to list the files in directory.  fileName is a string and contains the file extersion.  I like to know a easy way to parse filename and get last 3 char (extension)
0
Comment
Question by:pasta1
6 Comments
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
Hi pasta1,

File Extensions are usually 3 characters, but not always.  :)

There are quite a few ways to do this.  One simple, but effective way is this:

  char *Ext;

  Ext = strrchr ((FileName.c_str ()), '.');

Ext now points to the LAST period in the String, so the extension starts at Ext + 1.


Good Luck,
Kent
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 50 total points
Comment Utility
>>> fileName is a string and contains the file extersion.

In C++ you can do:

    string filename = "anypath\\anydir/anyname.anyext";
    string fileext;
    int pos = (int) filename.rfind('.');
    if (pos != string::npos)
         fileext = filename.substr(pos + 1);

Regards, Alex
0
 
LVL 4

Expert Comment

by:Ichijo
Comment Utility
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
If it was possible that the file has no extension you should do:

    string filename = "anypath\\anydir/anyname.anyext";
    string fileext;
    int pos1 = (int) filename.rfind('.');
    int pos2 = (int) filename.find_last_of("/\\:"); // find any of /  \  or :
    if (pos1 != string::npos && (pos2 == string::npos || pos2 < pos1) )
         fileext = filename.substr(pos1 + 1);
0
 

Author Comment

by:pasta1
Comment Utility
I can't seem to able to get it to work.  strchr: none of the 2 overloads could convert all the argument types
0
 

Author Comment

by:pasta1
Comment Utility
I have filenames only and no paths in the file name.  The filename also have multiple periods ex test.test.test.txt  so I hoping to parse filename for the chars after the last period
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now