String Manipulation Question from a Newbie!

Discussion in 'C++' started by Morris.C, Sep 8, 2005.

  1. Morris.C

    Morris.C Guest

    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
     
    Morris.C, Sep 8, 2005
    #1
    1. Advertising

  2. Morris.C

    Kai-Uwe Bux Guest

    Morris.C wrote:

    > 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
     
    Kai-Uwe Bux, Sep 8, 2005
    #2
    1. Advertising

  3. Morris.C

    Guest

    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
     
    , Sep 8, 2005
    #3
  4. Morris.C

    Morris.C Guest

    "Morris.C" <> wrote in news:Xns96CB969048A22mrncbigpondcom@
    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");
     
    Morris.C, Sep 8, 2005
    #4
  5. Morris.C wrote:
    > 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
    }
     
    Maxim Yegorushkin, Sep 8, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    8
    Views:
    478
    Matthew
    Apr 24, 2004
  2. John Salerno
    Replies:
    8
    Views:
    6,305
    Caleb Hattingh
    Mar 30, 2006
  3. Sengly
    Replies:
    4
    Views:
    305
    John Machin
    Jun 8, 2008
  4. Franklin
    Replies:
    5
    Views:
    363
    Franklin
    Feb 28, 2009
  5. Chris Roos

    String Manipulation Nuby Question

    Chris Roos, May 6, 2005, in forum: Ruby
    Replies:
    12
    Views:
    216
    James Britt
    May 6, 2005
Loading...

Share This Page