Basename

This is a discussion on Basename within the C++ Programming forums, part of the General Programming Boards category; Hi! What is the easy way to create a function in C++ which will return the basename from a string ...

  1. #1
    Fab
    Fab is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    Basename

    Hi!

    What is the easy way to create a function in C++ which will return the basename from a string i.e.

    from "C:/tutu/tata/test" I want to get "test"

    Thanks!

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You could try string::rfind, searches a string starting from the back. Check out this totally awesome code
    Code:
    #include <string>
    #include <iostream>
    int main(int argc, char *argv[])
    {
    	//You'll note how cool it is the lines line up
    	std::string stuff("C:/woot/rawr/whatev/test");
    	std::string::size_type pos = stuff.rfind("/");
    
    
    	if(pos != std::string::npos)
    		std::cout << stuff.substr(pos + 1, stuff.length() - pos + 1) << "\n";
    	else std::cout << "Invalid path\n";
    	return 0;
    }

  3. #3
    Fab
    Fab is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Thanks I tried that but I get the same in input and ouput doing

    Code:
    int pos = name.rfind("/"); 
    std::string basename = name.substr(pos + 1, name.length() - pos + 1);
    Maybe it's the "/"?

    Actually it is more complicated because I have

    "C:\SMG\Projects\BoreholeTut\GRDFILE.grd\D1SF.grid " and I want to obtain "D1SF"

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    #include <string>
    #include <iostream>
    int main(int argc, char *argv[])
    {
    	// You'll note the failure to make aesthetically awesome code here <sad>
    	std::string stuff("C:\\SMG\\Projects\\BoreholeTut\\GRDFILE.grd\\D1SF.grid");
    	std::string::size_type pos[2];
    		pos[0] = stuff.rfind("\\");
    		pos[1] = stuff.rfind(".");
    
    
    	if(pos[0] != std::string::npos && pos[1] != std::string::npos)
    		std::cout << stuff.substr(pos[0] + 1, stuff.length() - pos[1] - 1) << "\n";
    	else std::cout << "Invalid path\n";
    	return 0;
    }

  5. #5
    Fab
    Fab is offline
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    if I put a single slash "\" in the rfind search as it should be (you put two slashes in your example) I get a compile error "newline constant" why?

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Something about, maybe, escape sequences: http://www.cppreference.com/escape_sequences.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: conflicting types for 'basename'
    By samf in forum C Programming
    Replies: 3
    Last Post: 09-20-2007, 10:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21