Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

deleting the last char of every line

This topic is 5072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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
while(fin >> reader)
{
reader.erase( reader.end()-1);
fout << reader << endl;
};

i have that but it wont work.

(yes i have done all the opening and such nessisary)

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!