Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > remove of string

Reply
Thread Tools

remove of string

 
 
Mike
Guest
Posts: n/a
 
      04-22-2010
Hi
of a char: char filepath[255];
I should remove the last two words: lin.xpl
it's a path alike: blal/blaald/lin.xpl so I need something like: blal/
blaald/
How should I do that?
Many thanks
Michael
 
Reply With Quote
 
 
 
 
Fred Zwarts
Guest
Posts: n/a
 
      04-22-2010
"Mike" <> wrote in message
news:9efc2209-0190-4c7b-8b54-
> Hi
> of a char: char filepath[255];
> I should remove the last two words: lin.xpl
> it's a path alike: blal/blaald/lin.xpl so I need something like:
> blal/ blaald/
> How should I do that?
> Many thanks
> Michael


filepath[12] = 0;
 
Reply With Quote
 
 
 
 
Fred Zwarts
Guest
Posts: n/a
 
      04-23-2010
OK, an attempt for a more serious reply.
The first reply was trivial, because of the very vague and contradictionary question.

"Fred Zwarts" <> wrote in message
news:hqph40$fp1$
> "Mike" <> wrote in message
> news:9efc2209-0190-4c7b-8b54-
>> Hi
>> of a char: char filepath[255];
>> I should remove the last two words: lin.xpl


What exactly do you think defines the term "word" here.
Words are normally separated by word separators.
What do you consider a word separator?
Only a '.', as in the example, or also a '-', a '/', a ' ', a '_', etc.?
Are you sure that you always want to remove the last two words?
Even in the case that the string is e.g.
"/ect/bla.conf.original" -> "/etc/bla."
or "/etc/init.d/rc5.d/S10product" -> /etc/init.d/rc."?
or "/usr/local/bin" -> "/usr/".
What if there is a word separator at the end, or if there are two successive word separators as in "/etc/..".

>> it's a path alike: blal/blaald/lin.xpl so I need something like:
>> blal/ blaald/


This suggests that you do not want to remove the last two words,
but everything following the last '/', which is something very different.
(But you do not say so.)
Do you only want to consider the '/', or also the '\' (as it is used in Windows)?

>> How should I do that?


First try to formulate what you want to do exactly.
Then see if you really want to do this with an array of char with a fixed length,
or with the more flexible std::string type, which has methods to find the
first, or the last position of a given set of characters within the string,
to extract substrings, and more string manipulation methods.

>> Many thanks
>> Michael

>
> filepath[12] = 0;

 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      04-23-2010
Hi
filepath is a char * which i need for loading sound. Now i need the
full path to the sound for loading. So
remove all after the last / but add sounds/ban.wav.
Something's wrong below.
Many thanks again
Michael


std:: string s = filepath;
std::string t = s.substr(0, s.find_last_of('/') + 1);
char *conv;
conv = new char[t.length() +1];
strcpy(conv, t.c_str());
char str[255] = "sounds/BAN11.wav";
strcat(conv, str);
alutLoadWAVFile((ALbyte*) conv, &format, &data, &size, &freq,
&loop);


 
Reply With Quote
 
Fred Zwarts
Guest
Posts: n/a
 
      04-23-2010
"Mike" <> wrote in message
news:00999645-8a24-4246-960b-
> Hi
> filepath is a char * which i need for loading sound. Now i need the
> full path to the sound for loading. So
> remove all after the last / but add sounds/ban.wav.
> Something's wrong below.
> Many thanks again
> Michael
>
>
> std:: string s = filepath;
> std::string t = s.substr(0, s.find_last_of('/') + 1);


A bit dangerous. What happens if there is no '/' in s?
Is an empty t intended?

> char *conv;
> conv = new char[t.length() +1];
> strcpy(conv, t.c_str());
> char str[255] = "sounds/BAN11.wav";
> strcat(conv, str);
> alutLoadWAVFile((ALbyte*) conv, &format, &data, &size, &freq, &loop);


These last 7 lines can be written easier as

t += "sounds/BAN11.wav";
alutLoadWAVFile((ALbyte*) t.c_str (), &format, &data, &size, &freq, &loop);

with the side effect that t has a different value at the end. If that is not what you want:

const string conv = t + "sounds/BAN11.wav";
alutLoadWAVFile((ALbyte*) conv.c_str (), &format, &data, &size, &freq, &loop);
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      04-23-2010
Thanks Stuart. Still something is wrong....VB.NET is so much easier
for strings....
Many thanks again
Michael
------
XPLMGetPluginInfo( XPLMGetMyID(), NULL,filepath, NULL,NULL);
std:: string str = filepath;
std::string::size_type n = str.find_last_of('/');
if(n == std::string::npos) {
str.replace(n,str.length(),"sounds/BAN11.wav");
}
alutLoadWAVFile((ALbyte*) str.c_str (), &format, &data, &size,
&freq, &loop);
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-25-2010
On Fri, 2010-04-23, Mike wrote:
> Hi
> filepath is a char * which i need for loading sound. Now i need the
> full path to the sound for loading. So
> remove all after the last / but add sounds/ban.wav.


You seem to be after the function commonly known as dirname.
It exists in the Unix shell, and for example in Python as
os.path.dirname(). Does not exist in C++, but

(a) finding its documentation helps a lot when you designin your own
(b) and yours should probaly be called dirname()
(c) there's probably one in Boost.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Httpmodule still executed even if remove using <remove> config entry Simon-Pierre Jarry ASP .Net 2 08-10-2005 11:22 AM
How to remove items from add/remove list please Caractucus Potts Computer Support 5 07-03-2005 10:31 PM
Q: remove a certain string in string =?Utf-8?B?SklNLkgu?= ASP .Net 2 02-28-2005 02:02 AM
remove a string from within a string soni29 Javascript 2 08-07-2004 03:23 AM
Add/Remove Program Glitch: Asks If I Want To To Remove Wrong Program ? Robert11 Computer Support 6 08-02-2004 09:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57