Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<string>
- int main()
- {
- std::string fn = "filename.conf";
- if(fn.substr(fn.find_last_of(".") + 1) == "conf") {
- std::cout << "Yes..." << std::endl;
- } else {
- std::cout << "No..." << std::endl;
- }
- }
- std::string filename("filename.conf");
- std::string::size_type idx;
- idx = filename.rfind('.');
- if(idx != std::string::npos)
- {
- std::string extension = filename.substr(idx+1);
- }
- else
- {
- // No extension found
- }
- std::string GetFileExtension(const std::string& FileName)
- {
- if(FileName.find_last_of(".") != std::string::npos)
- return FileName.substr(FileName.find_last_of(".")+1);
- return "";
- }
- System::String^ GetFileExtension(System::String^ FileName)
- {
- int Ext=FileName->LastIndexOf('.');
- if( Ext != -1 )
- return FileName->Substring(Ext+1);
- return "";
- }
- #include <ctype.h>
- #include <string.h>
- int main()
- {
- char filename[] = "apples.bmp";
- char extension[] = ".jpeg";
- if(compare_extension(filename, extension) == true)
- {
- // .....
- } else {
- // .....
- }
- return 0;
- }
- bool compare_extension(char *filename, char *extension)
- {
- /* Sanity checks */
- if(filename == NULL || extension == NULL)
- return false;
- if(strlen(filename) == 0 || strlen(extension) == 0)
- return false;
- if(strchr(filename, '.') == NULL || strchr(extension, '.') == NULL)
- return false;
- /* Iterate backwards through respective strings and compare each char one at a time */
- for(int i = 0; i < strlen(filename); i++)
- {
- if(tolower(filename[strlen(filename) - i - 1]) == tolower(extension[strlen(extension) - i - 1]))
- {
- if(i == strlen(extension) - 1)
- return true;
- } else
- break;
- }
- return false;
- }
Add Comment
Please, Sign In to add comment