Erase Last Character of basic::string Variable

Discussion in 'C++' started by Mike Copeland, Dec 11, 2012.

  1. 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
     
    Mike Copeland, Dec 11, 2012
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Dec 11, 2012
    #2
    1. Advertising

  3. Mike Copeland

    SG Guest

    On Dec 11, 1:11 am, (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)) {
    :::
    }
     
    SG, Dec 11, 2012
    #3
  4. Mike Copeland

    Jorgen Grahn Guest

    On Tue, 2012-12-11, SG wrote:
    > On Dec 11, 1:11 am, (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 .
     
    Jorgen Grahn, Dec 11, 2012
    #4
  5. Mike Copeland

    Bo Persson Guest

    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
     
    Bo Persson, Dec 15, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Mark
    Replies:
    6
    Views:
    70,788
    Tim Chase
    Sep 28, 2006
  2. Replies:
    0
    Views:
    235
  3. Brand Bogard

    8 bit character string to 16 bit character string

    Brand Bogard, May 25, 2006, in forum: C Programming
    Replies:
    8
    Views:
    742
    those who know me have no need of my name
    May 28, 2006
  4. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    369
    Pete Becker
    Mar 30, 2006
  5. Johny
    Replies:
    8
    Views:
    394
Loading...

Share This Page