Erase Last Character of basic::string Variable

M

Mike Copeland

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
 
V

Victor Bazarov

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
 
S

SG

   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)) {
:::
}
 
J

Jorgen Grahn

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
 
B

Bo Persson

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
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top