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\mo repath\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 4 1348
"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\mo repath\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
> 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\mo repath\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::strin g". 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::ostringst ream" with "std::copy< >()" and some magic "std::ostream_i terator<>".
This will give you a "std::strin g" by means of "std::ostringst ream::str()".
Cheers,
Philipp.
Some Clown wrote: For example, I've got "C:\somepath\mo repath\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\\m orepath\\filena me
or use:
C:/somepath/morepath/filename
Brian Rodenborn
> 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\mo repath\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::filesyst em 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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ying Yang |
last post by:
Hi,
whats the difference between:
char* a = new char;
char* b = new char;
char c ;
|
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. However the
declaration:
int main(int argc, char** argv)
is common.
|
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 variable **var make it an array of pointers?
I realize that 'char **var' is a pointer to a pointer of type char (I hope).
And I realize that with var, var is actually a memory address (or at
|
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);
|
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" };
doSomething(strs, 3);
| |
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
|
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);
|
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;
|
by: MN |
last post by:
I have a question :
How to understand the mean of char** type ?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |