char[] element editing question...

S

Some Clown

If I have an array like so:

lineItems[MAX][MAX2];

which is of type char[] and I've filled it up with some stuff (filenames and
paths mostly,) how would I go about replacing a portion of each element?
For example, I've got "C:\somepath\morepath\filename" in lineItems[0] and I
want to replace it with "F:\shorterpath\filename"... I've thought about
creating another array, looping through and copying portions out to the new
array, but I'm not sure how efficient that would be. Is there some sort of
functionality somewhere to allow me to strip 'n' characters off of an array
element of type char[] and combine with new elements, then put back?

Like:

Loop 'n' times
strip 'i' number of chars from beginning of each element
replace with 'j' and remove whitespace
repeat
 
V

Victor Bazarov

Some Clown said:
If I have an array like so:

lineItems[MAX][MAX2];

which is of type char[] and I've filled it up with some stuff (filenames and
paths mostly,) how would I go about replacing a portion of each element?
For example, I've got "C:\somepath\morepath\filename" in lineItems[0] and I
want to replace it with "F:\shorterpath\filename"... I've thought about
creating another array, looping through and copying portions out to the new
array, but I'm not sure how efficient that would be. Is there some sort of
functionality somewhere to allow me to strip 'n' characters off of an array
element of type char[] and combine with new elements, then put back?

Don't use character arrays, use 'std::string', life's going to be much
easier.

V
 
P

Philipp Bachmann

which is of type char[] and I've filled it up with some stuff (filenames and
paths mostly,) how would I go about replacing a portion of each element?
For example, I've got "C:\somepath\morepath\filename" in lineItems[0] and I
want to replace it with "F:\shorterpath\filename"...

As long as your data is likely to change that way, consider using
a more sophisticated data structure to handle your information than
fields of "char" or "std::string". You could e.g. use "std::list< std::string >",
where the boundaries between list entries are equivalent to your character
"/". Now what you've called "removing whitespace", which is an expensive
operation with any field-like data structure, isn't necessary at all any more.

After you know that your data won't change you can stream it into
a "std::eek:stringstream" with "std::copy<>()" and some magic "std::eek:stream_iterator<>".
This will give you a "std::string" by means of "std::eek:stringstream::str()".

Cheers,
Philipp.
 
D

Default User

Some said:
For example, I've got "C:\somepath\morepath\filename" in lineItems[0] and I
want to replace it with "F:\shorterpath\filename"...

Besides what others told you about using std::string (I think I said
that in response to a previous question as well) the above strings are
unlikely to give you file names you think they will. The \ character
delineates an escape sequence in string literals.

You either need to double the backslash:

C:\\somepath\\morepath\\filename

or use:

C:/somepath/morepath/filename





Brian Rodenborn
 
O

Old Wolf

If I have an array like so:
lineItems[MAX][MAX2];

which is of type char[] and I've filled it up with some stuff (filenames and
paths mostly,) how would I go about replacing a portion of each element?
For example, I've got "C:\somepath\morepath\filename" in lineItems[0] and I
want to replace it with "F:\shorterpath\filename"... I've thought about
creating another array, looping through and copying portions out to the new
array, but I'm not sure how efficient that would be. Is there some sort of
functionality somewhere to allow me to strip 'n' characters off of an array
element of type char[] and combine with new elements, then put back?

The boost::filesystem library has functions for doing this sort of thing,
you could check that out. (www.boost.org)
(Possible caveat: it takes forever to compile, so if you have a slow
CPU and a compiler without precompiled headers, don't bother)
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top