Link to home
Start Free TrialLog in
Avatar of bert1
bert1

asked on

Remove characters from string.

I have this string: C:\Dir1\Dir4\AnotherDir\*.*
or this one C:\Dir\Test\*.dll

I want to cut the last *.* and *.dll with a function and save C:\Dir\Test to a new string.
The function should search for the last '\' and remove all characters after that ??
I am using Borland C++ 5.02 (winapi-code if possible). How would my function look like ??
ASKER CERTIFIED SOLUTION
Avatar of nil_dib
nil_dib

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bert1
bert1

ASKER

Have you tried it so it work?? I cant get it to work ...
Heres a version with STL (not quick enough with my answer!):

#include <iostream>
#include <algorithm>

#include <string>

using namespace std;

std::string
find_directory_root(const string& path)
{
    string result = path;
    string seperator = "\\";
    string::iterator i = find_end(result.begin(), result.end(),
                            seperator.begin(), seperator.end());
    if (i != result.end())
    {
        result = string(result.begin(), i);
    }

    return result;
}
void main()
{
    string s = "c:\\std\\test\\*.dll";

    cout << find_directory_root(s) << endl;
}
sorry ...
copy paste bug.
this one works fine:

char szBuf[] = "C:\\Dir1\\Dir4\\AnotherDir\\*.*";
char szNewBuf[256];
int nPos ;

char * psz = strrchr( szBuf, '\\' );
nPos = psz - szBuf + 1;
strncpy(szNewBuf,szBuf,nPos);

>>not quick enough with my answer
sorry jason.
bert1: jasonclarkes comment is the proper C++ way ...


Avatar of bert1

ASKER

jasonclark!

I got an error: 'string' is not an member of 'std'.

What can I do to fix that ?
Hmm, I'm not sure about the status of string in the Borland Compiler, if that is the only complaint, then you can just change std::string in the return type to string.
Avatar of bert1

ASKER

Ok that worked but now I got the error : undefined symbol 'iterator'.

string::iterator i = find_end(result.begin(), result.end(),
          seperator.begin(), seperator.end());

another error : result.begin() is not a member of 'string'

:-(  Hmm seems like nil_dib version worked better ... I dont know why I got these errors ... I got #include <algorith.h> not <algorithm.h> (cause that file doesnt exist)

Thanks!
This would be because for some reason you don't have access to the STL, note that the #include statements, should not have the .h on the end for STL headers, i.e.

you should have:

#include <algorithm>
#include <string>

Maybe another expert who knows the Borland Compiler can tell you how to get at the STL headers?