Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove characters from string.

Posted on 1999-07-13
9
Medium Priority
?
291 Views
Last Modified: 2010-04-16
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 ??
0
Comment
Question by:bert1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
9 Comments
 
LVL 4

Accepted Solution

by:
nil_dib earned 120 total points
ID: 1199945
char szBuf[] = "C:\\Dir1\\Dir4\\AnotherDir\\*.*";
char szNewBuf[256];
int nPos ;

char * psz = strrchr( string, '\\' );
nPos = psz - szBuf + 1
strncpy(szNewBuf,szBuf,nPos);
// szNewBuf = "C:\\Dir1\\Dir4\\AnotherDir\\"
0
 

Author Comment

by:bert1
ID: 1199946
Have you tried it so it work?? I cant get it to work ...
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199947
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;
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:nil_dib
ID: 1199948
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);

0
 
LVL 4

Expert Comment

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


0
 

Author Comment

by:bert1
ID: 1199950
jasonclark!

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

What can I do to fix that ?
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199951
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.
0
 

Author Comment

by:bert1
ID: 1199952
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!
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199953
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?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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