Solved

Like to know how to get the file name extenssion

Posted on 2007-12-03
6
218 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
ID: 20397597
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
ID: 20397631
>>> 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
ID: 20397664
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20397685
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
ID: 20397686
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
ID: 20397722
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

862 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

28 Experts available now in Live!

Get 1:1 Help Now