473,473 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[Fwd: <string>.replace]



-------- Original Message --------
Subject: <string>.replace
Date: Fri, 15 Oct 2004 11:07:19 -0500
From: Billy N. Patton <b-******@ti.com>
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, b-******@ti.com

--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodlogy Group
Dallas, Texas, 214-480-4455, b-******@ti.com
Jul 22 '05 #1
1 2167

"Billy N. Patton" <b-******@ti.com> wrote in message
news:ck**********@home.itg.ti.com...


-------- Original Message --------
Subject: <string>.replace
Date: Fri, 15 Oct 2004 11:07:19 -0500
From: Billy N. Patton <b-******@ti.com>
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

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-******@ti.com

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

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
1
by: Mark | last post by:
I want to replace the following line: sscanf(mybuf,"%s=%s\n",sz1,sz2); with something that produces the same effect, only with dynamic storage i.e string s which is safer. I might try this:...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
5
by: Minkoo Seo | last post by:
Hi list. I'd like to print a line "" given a vector<string> containing "A", "BC", "D", "EF". The following code is what I've written for this purpose: #include <iostream> #include <vector>
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
8
by: Carmen Sei | last post by:
it seem to me that when doing include - #include <string.h- is CRT #inlcude <string- is C++ standard library Is that true those header with .h extension is CRT and those without extension...
4
by: parez | last post by:
Hi, I am trying to serialize List<List<string>. With the following code public List<List<string>DataRows { get; set; }
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.