Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Erase Last Character of basic::string Variable

Reply
Thread Tools

Erase Last Character of basic::string Variable

 
 
Mike Copeland
Guest
Posts: n/a
 
      12-11-2012
How do I remove the last character of a basic::string? Specifically,
I'm using fgets to read lines from a text file, and each ends with the
characters '\10' and '\0'. When I assign the c-string variable to a
basic::string the '\0' is dropped, but the '\10' remains. I want to
erase this character before subsequent processing, but I can't find any
way to do so. I've tried:

string::iterator itr = str.end()-1;
if(*itr == '\10')
str.erase(*itr, 1);

but that doesn't work. Trying to use rbegin() doesn't compile. 8<{{
Please advise. TIA
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-11-2012
On 12/10/2012 7:11 PM, Mike Copeland wrote:
> How do I remove the last character of a basic::string? Specifically,
> I'm using fgets to read lines from a text file, and each ends with the
> characters '\10' and '\0'. When I assign the c-string variable to a
> basic::string the '\0' is dropped, but the '\10' remains. I want to
> erase this character before subsequent processing, but I can't find any
> way to do so. I've tried:
>
> string::iterator itr = str.end()-1;
> if(*itr == '\10')
> str.erase(*itr, 1);


What do you think that does? RTFM on 'std::string::erase' member functions.

Have you tried debugging your program to see whether your 'str.erase' is
in fact being called when you need it? And if it does get called, what
is the actual effect? Is '\10' in fact the right character to be
looking for?

> but that doesn't work. Trying to use rbegin() doesn't compile. 8<{{
> Please advise. TIA


Your program has at least one bug. Debug your program.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      12-11-2012
On Dec 11, 1:11*am, (E-Mail Removed) (Mike Copeland) wrote:
> * *How do I remove the last character of a basic::string? *Specifically,
> I'm using fgets to read lines from a text file, and each ends with the
> characters '\10' and '\0'.


You mean 10 or '\n' instead of '\10'.

fgets gives you the line feed because this is the only way for you to
find out whether the line was loaded completely or the buffer was just
to small to hold the complete line. If you use std::getline instead,
you don't have to worry about this. So, instead of fgets'ing into a
char buffer and then converting it into a std::string, read directly
into a std::string via std::getline from the <string> header.

Doing this in a loop is easy

std::ifstream file ...;
std::string line;
while (getline(file,line)) {
:::
}

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      12-11-2012
On Tue, 2012-12-11, SG wrote:
> On Dec 11, 1:11*am, (E-Mail Removed) (Mike Copeland) wrote:
>> * *How do I remove the last character of a basic::string? *Specifically,
>> I'm using fgets to read lines from a text file, and each ends with the
>> characters '\10' and '\0'.

>
> You mean 10 or '\n' instead of '\10'.
>
> fgets gives you the line feed because this is the only way for you to
> find out whether the line was loaded completely or the buffer was just
> to small to hold the complete line.


And sometimes it doesn't give a '\n' even if the line is complete.

OP: get a copy of any half-decent fgets(3) manual page -- it describes
the details you need to know to use fgets safely!

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      12-15-2012
Mike Copeland skrev 2012-12-11 01:11:
> How do I remove the last character of a basic::string? Specifically,
> I'm using fgets to read lines from a text file, and each ends with the
> characters '\10' and '\0'. When I assign the c-string variable to a
> basic::string the '\0' is dropped, but the '\10' remains. I want to
> erase this character before subsequent processing, but I can't find any
> way to do so. I've tried:
>
> string::iterator itr = str.end()-1;
> if(*itr == '\10')
> str.erase(*itr, 1);
>
> but that doesn't work. Trying to use rbegin() doesn't compile. 8<{{
>


How about

if (str.back() == 10)
str.resize(str.size() - 1);



Bo Persson



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace the last (and only last) character in a string? Johny Python 8 05-03-2007 09:24 PM
remove the last character or the newline character? Daniel Mark Python 6 09-28-2006 02:40 PM
erase vs. erase al.cpwn@gmail.com C++ 7 03-30-2006 11:45 AM
How do i erase all information in my orkut and erase the link to the orkut account? mountbatten@gmail.com Computer Support 1 10-31-2005 12:03 AM
How do I erase a file that wont let me erase it? Dale Custer Computer Support 4 07-06-2003 09:48 AM



Advertisments