remove of string

M

Mike

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
 
F

Fred Zwarts

Mike said:
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;
 
F

Fred Zwarts

OK, an attempt for a more serious reply.
The first reply was trivial, because of the very vague and contradictionary question.

Fred Zwarts said:
Mike said:
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/..".

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)?

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;
 
M

Mike

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);
 
F

Fred Zwarts

Mike said:
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);
 
M

Mike

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);
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top