Sign in to follow this  
raptorstrike

deleting the last char of every line

Recommended Posts

The following is one way of doing it; I by no means assert that it is the best or most efficient:

You would have to read a line out of your istream to a temporary string variable, then use the erase method of the string class like so:

ifstream fin( "test.txt" ); // your input file
ofstream fout( "out.txt" ); // outputs lines without last character

string temp;

getline( fin, temp );
while( fin ) {
temp.erase( temp.end() - 1 );
fout << temp << endl;
getline( fin, temp );
} // while




Regards,
jflanglois

Share this post


Link to post
Share on other sites
Well, what do you mean it doesn't work?

One thing I can see it not doing is getting a whole line from fin, because you are using the stream extraction operator, which stops at the first whitespace character as opposed getline(), which stops at \n (unless otherwise specified)

Also, what is reader?

Regards,
jflanglois

Share this post


Link to post
Share on other sites
Quote:
Original post by jflanglois
You would have to read a line out of your istream to a temporary string variable, then use the erase method of the string class like so:

*** Source Snippet Removed ***
You can simplify that loop a little, since getline returns the stream object:
ifstream fin( "test.txt" ); // your input file
ofstream fout( "out.txt" ); // outputs lines without last character

string temp;

while( getline( fin, temp ) ) {
temp.erase( temp.end() - 1 );
fout << temp << endl;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this