473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

char[] element editing question...

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
Jul 22 '05 #1
4 1325
"Some Clown" <no***@nowhere.net> wrote...
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
Jul 22 '05 #2
> 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::ostringstream" with "std::copy<>()" and some magic "std::ostream_iterator<>".
This will give you a "std::string" by means of "std::ostringstream::str()".

Cheers,
Philipp.
Jul 22 '05 #3
Some Clown wrote:
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
Jul 22 '05 #4
> 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)
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Ying Yang | last post by:
Hi, whats the difference between: char* a = new char; char* b = new char; char c ;
21
by: Bret | last post by:
I'm curious why char** argv is acceptable in the main() declaration. In the comp.lang.c FAQ (question 6.18) it says that pointers to pointers and pointers to an array are not interchangable. ...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
12
by: confusedcoder | last post by:
Suppose I have the following function: void doSomething(char** array, int size); Normally, I would call the function with an array of, say, 3 elements... char* strs = { "str1","str2","str3"...
30
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
18
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
13
by: S S | last post by:
Hi I have a very basic question, but it's a good one. Below is the code fragment. struct ltstr { bool operator()(const char* s1, const char* s2) const { return strcmp(s1, s2) < 0;
16
by: MN | last post by:
I have a question : How to understand the mean of char** type ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.