Solved

Like to know how to get the file name extenssion

Posted on 2007-12-03
6
222 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 71
Error creating a new C++ project in ,net 20 36
Why  my code (program) build with old compiler? 11 47
How do i run a c++ file? 15 30
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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