Thread: Trouble with strings

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    27

    Trouble with strings

    Below there's a function that given a string of the form "../data/afile.txt" separates the path("../data/") from the file name("afile.txt"). I can get the path succesfully, but unfortunately I can't get the file name correctly.

    Code:
    void SplitStringToFileAndPath(char **file, char **path, const char *string)
    {
        int len = strlen(string);
        int index = 0, j = 0;
    
        // search the string from the end to beggining to find the last '/' or '\' character
        for(int i = len; string[i] != '/' && string[i] != '\\'; i--)
            j++;
    
        // find the position from the beginning of the last '/' or '\'
        index = len - j + 1;
    
        // extract path
        //delete *path;
        *path = new char[index + 1];
    
        // copy path
        for(int i = 0; i < index; i++)
            (*path)[i] = string[i];
    
        (*path)[index] = '\0';
    
        // extract filename
        //delete *file;
        *file = new char[len - index + 1];
    
        for(int i = 0; i < (len - index); i++)
            (*file)[i] = string[(len - index) + i];
    
        (*file)[len - index] = '\0';
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you have a particular reason for mucking about with allocating char arrays, as opposed to say passing a reference to a std::string to store your results?

    You should simplify the maths
    One copy is
    for ( i = 0, j = 0; j < index ; i++, j++ )

    The other is
    for ( i = 0, j = index+1 ; j < len ; i++, j++ )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why something so big complicated and replying on C strings and pointers? Here is simply how I would would do it:
    Code:
    #ifndef GET_PATH_20100802_1812
    #define GET_PATH_20100802_1812
    
    #include <string>
    
    namespace Stuff
    {
    	namespace File
    	{
    		std::wstring GetFile(const std::wstring& strFullPath)
    		{
    			auto pos = strFullPath.rfind(L'\\');
    			if (pos == std::wstring::npos)
    				return L"";
    			return strFullPath.substr(pos + 1);
    		}
    
    		std::wstring GetPath(const std::wstring& strFullPath)
    		{
    			auto pos = strFullPath.rfind(L'\\');
    			if (pos == std::wstring::npos)
    				return L"";
    			return strFullPath.substr(0, pos);
    		}
    	}
    }
    
    #endif
    Combine those two and you have what you want.
    Last edited by Elysia; 02-27-2011 at 07:55 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    27
    Thanks for help, I corrected the function.

    I use C strings just for training.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Array of Strings
    By nair in forum C Programming
    Replies: 6
    Last Post: 10-09-2010, 11:10 AM
  2. swapping strings (which are stored in an array)
    By nair in forum C Programming
    Replies: 2
    Last Post: 10-04-2010, 01:27 PM
  3. Replies: 1
    Last Post: 09-20-2010, 11:17 AM
  4. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  5. trouble with strings and characters
    By mhenderson in forum C Programming
    Replies: 7
    Last Post: 08-02-2006, 01:29 PM