[Fwd: <string>.replace]

B

Billy N. Patton

-------- Original Message --------
Subject: <string>.replace
Date: Fri, 15 Oct 2004 11:07:19 -0500
From: Billy N. Patton <[email protected]>
Organization: Texas Instruments
Newsgroups: alt.comp.lang.learn.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(string& cmd,vector<string>& 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.length()-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, (e-mail address removed)

--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, (e-mail address removed)
 
H

Howard

Billy N. Patton said:
-------- Original Message --------
Subject: <string>.replace
Date: Fri, 15 Oct 2004 11:07:19 -0500
From: Billy N. Patton <[email protected]>
Organization: Texas Instruments
Newsgroups: alt.comp.lang.learn.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(string& cmd,vector<string>& 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.length()-1,1,"\n");

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...?
cout << "'" << s << "'\n";

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
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top