[Fwd: <string>.replace]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Billy N. Patton

    [Fwd: <string>.replace]



    -------- Original Message --------
    Subject: <string>.replac e
    Date: Fri, 15 Oct 2004 11:07:19 -0500
    From: Billy N. Patton <b-patton@ti.com>
    Organization: Texas Instruments
    Newsgroups: alt.comp.lang.l earn.c-c++

    I'm trying to remove the \n from a string.
    If I just simply locate teh char and replace it with \0 then the
    destructor should only delete up to the \0 and leave 1 char unrecovered.

    Here is what I've tried several times

    From my documentation
    basic_string &replace( size_type index, size_type num, const
    basic_string &str );

    bool ExCommand(strin g& cmd,vector<stri ng>& ret)
    {
    string s;
    static char buf[BUFSZ];
    FILE *ptr = NULL;

    if (cmd.empty()) return false;

    if ((ptr = popen(cmd.c_str (), "r")) NE NULL)
    {
    while (fgets(buf, BUFSZ, ptr) NE NULL)
    {
    s = buf;
    s.replace(s.len gth()-1,1,"\n");
    cout << "'" << s << "'\n";
    ret.push_back(s );
    }
    pclose(ptr);
    }
    else
    {
    return false;
    }
    return true;
    }


    The cout prints :
    './TEST.cxx
    '

    It's not replacing the \n

    I've tried s.length() without the -1 with the same results.

    --
    ___ _ ____ ___ __ __
    / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    /___/
    Texas Instruments ASIC Circuit Design Methodlogy Group
    Dallas, Texas, 214-480-4455, b-patton@ti.com

    --
    ___ _ ____ ___ __ __
    / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    /___/
    Texas Instruments ASIC Circuit Design Methodlogy Group
    Dallas, Texas, 214-480-4455, b-patton@ti.com
  • Howard

    #2
    Re: &lt;string&gt;. replace]


    "Billy N. Patton" <b-patton@ti.com> wrote in message
    news:ckpcoj$jl7 $2@home.itg.ti. com...[color=blue]
    >
    >
    > -------- Original Message --------
    > Subject: <string>.replac e
    > Date: Fri, 15 Oct 2004 11:07:19 -0500
    > From: Billy N. Patton <b-patton@ti.com>
    > Organization: Texas Instruments
    > Newsgroups: alt.comp.lang.l earn.c-c++
    >
    > I'm trying to remove the \n from a string.
    > If I just simply locate teh char and replace it with \0 then the
    > destructor should only delete up to the \0 and leave 1 char unrecovered.
    >
    > Here is what I've tried several times
    >
    > From my documentation
    > basic_string &replace( size_type index, size_type num, const
    > basic_string &str );
    >
    > bool ExCommand(strin g& cmd,vector<stri ng>& ret)
    > {
    > string s;
    > static char buf[BUFSZ];
    > FILE *ptr = NULL;
    >
    > if (cmd.empty()) return false;
    >
    > if ((ptr = popen(cmd.c_str (), "r")) NE NULL)
    > {
    > while (fgets(buf, BUFSZ, ptr) NE NULL)
    > {
    > s = buf;
    > s.replace(s.len gth()-1,1,"\n");[/color]

    According to my books, that call replaces the last character of the string
    with '\n'. The first parameter is where to start: length-1 is the last
    characeter. The second parameter is how many to replace: 1, just that last
    character. The last parameter is the string to replace it *with*: '\n'. So
    you change the last character of the string to a '\n' character. I'm
    guessing that the last character of the string already *was* the '\n'
    character...?
    [color=blue]
    > cout << "'" << s << "'\n";[/color]

    Here, after writing out a single quote, and the string with its (new) CR/LF,
    you then output a single quote and another CR/LF. So what you see below is
    exactly what you asked for.


    Unfortunately, I only know what it's doing. I don't know how to do what you
    want without further study. :-(

    -Howard

    [color=blue]
    > ret.push_back(s );
    > }
    > pclose(ptr);
    > }
    > else
    > {
    > return false;
    > }
    > return true;
    > }
    >
    >
    > The cout prints :
    > './TEST.cxx
    > '
    >
    > It's not replacing the \n
    >
    > I've tried s.length() without the -1 with the same results.
    >
    > --
    > ___ _ ____ ___ __ __
    > / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    > / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    > /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    > /___/
    > Texas Instruments ASIC Circuit Design Methodlogy Group
    > Dallas, Texas, 214-480-4455, b-patton@ti.com
    >
    > --
    > ___ _ ____ ___ __ __
    > / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    > / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    > /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    > /___/
    > Texas Instruments ASIC Circuit Design Methodlogy Group
    > Dallas, Texas, 214-480-4455, b-patton@ti.com[/color]


    Comment

    Working...