String Manipulation Question from a Newbie!

M

Morris.C

I've had to do some editing of a C++ program that was origianlly created
by a departed colleague of mine.

What I need to do is change a certain part of a string. The string
contains a file name. The file name is taken from an environmental
variable.

This is what I did: (I'm not including any variable declarations.)

filename = getenv("FILENAME");
len = strlen(filename);
len1 = len - 4;
strncpy(filenamenew,filename,len1);
strcat(pch,"backup1");

Eg. If filename/FILENAME is a1b2c3d4.text, the variable 'pch' ends up
being a1b2c3d4.backup1.
The length of filename can change.
I know that I always want to delete the last four characters.

Now the problem: (He finally gets to the point!!)
'pch' ends up having gobbledoogook (ie.crap) between 'a1b2c3d4.' and
'backup1'.
Eg. a1b2c3d4.}@*backup1

Why?
Where is it coming from?
Is there a single function that will do this?
(I couldn't find one at http://www.cplusplus.com/ref/ !!)


Thanks,
Morris.C
Melbourne, Australia
 
K

Kai-Uwe Bux

Morris.C said:
I've had to do some editing of a C++ program that was origianlly created
by a departed colleague of mine.

What I need to do is change a certain part of a string. The string
contains a file name. The file name is taken from an environmental
variable.

This is what I did: (I'm not including any variable declarations.)

filename = getenv("FILENAME");
len = strlen(filename);
len1 = len - 4;
strncpy(filenamenew,filename,len1);

* what is filenamenew?
* where did you allocate it?
* what does it point to?
* how do you guarantee that after this operation, filenamenew is a
0-terminated string?
* how do you know that 0-termination is at the end of the chunk copied from
filename?
strcat(pch,"backup1");
pch?


Eg. If filename/FILENAME is a1b2c3d4.text, the variable 'pch' ends up
being a1b2c3d4.backup1.
The length of filename can change.
I know that I always want to delete the last four characters.

Now the problem: (He finally gets to the point!!)
'pch' ends up having gobbledoogook (ie.crap) between 'a1b2c3d4.' and
'backup1'.
Eg. a1b2c3d4.}@*backup1

Why?

filenamenew was not properly initialized. You copied an initial segment from
filename but you did not put in a 0 afterwards. Thus, when you do strcat(),
the function searches for the first 0 which is at some random location in
memory and puts "backup1" right behind the garbage.
Where is it coming from?
Is there a single function that will do this?

Well, you might consider using std::string. Not that I would know of a
member function that will do what you need. But it will take care of memory
management for you.



Best

Kai-Uwe Bux
 
R

ravinderthakur

this is because when you do
strcat(pch,"backup1");

the pch is already corrupted c string.

try printing the value of pch before the above statement and see what
it contains!!!


though it is not evident from you code i think what your problem is
that the

strncpy(filenamenew,filename,len1);

dosn't copy the null character to the filenamenew paramenter. so it is
not a valid c string. u should rather write:

strncpy(filenamenew,filename,len1);
filenamenew[len1] = '\0';//add the null character since strcat strncpy
dosn't adds it
strcat(pch,"backup1");

where i think pch is a pointer to filenamenew.



thanks
rt
 
M

Morris.C

202.12.162.100:

Oops, made a mistake when typing out the code.

filename = getenv("FILENAME");
len = strlen(filename);
len1 = len - 4;
strncpy(filenamenew,filename,len1);
strcat(filenamenew,"backup1");
 
M

Maxim Yegorushkin

Morris.C said:
I've had to do some editing of a C++ program that was origianlly created
by a departed colleague of mine.

What I need to do is change a certain part of a string. The string
contains a file name. The file name is taken from an environmental
variable.

This is what I did: (I'm not including any variable declarations.)

filename = getenv("FILENAME");
len = strlen(filename);
len1 = len - 4;
strncpy(filenamenew,filename,len1);

The preceding statement does not terminate filenamenew with zero. I
assume pch and filenamenew point to the same char[]. This is why the
following strcat does not find the end of the string where you expect
it to and append the suffix at a wrong place.
strcat(pch,"backup1");

You can fix your code as follows:

char const* filename = getenv("FILENAME");
size_t len = strlen(filename);
char new_name[PATH_MAX];
memcpy(new_name, filename, len - 4); // leave the last 4 characters
memcpy(new_name + len - 4, "backup1", sizeof("backup1")); // append the
suffix

Here are all sanity check omitted.

You can get safe code with less effort just by using std::string:

std::string name(getenv("FILENAME"));
if(name.size() > 4)
{
name.resize(name.size() - 4); // strip the last 4 chars
name += "backup1"; // append the suffix
}
else
{
// handle bad file name
}
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top