Is there a way in C++ to remove/trim a trailing new line from a text file?
For example
content content
content content
content content
<- this line in the text file is empty and needs to go ->
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Sure! One way to do it would be to read the file to a std::string
and then use any of the techniques described here:
C++ Remove new line from multiline string
then you could overwrite the file with the new result. Of course, this approach ain’t practical when dealing with very large files (let’s say, 2GB) but such thing is not a constraint according to your original question.
This thread also has great material on detecting new lines.