Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing std::endl to put out CR+LF instead of LF

I'm writing a program on a Linux platform that is generating text files that will be viewed on, inevitably, a Windows platform.

Right now, passing std::endl into a ostream generates the CR character only for newlines. Naturally, these text files look wrong in MS Notepad.

  1. Is there a way to change std::endl such that it uses CR+LF for newline instead of LF?
  2. I know I could write my own custom manipulator, like win_endl, for generating my own newlines, but I use the std::endl symbol in a lot of places, and like many programmers, have a tendency to do the thing that requires the least work possible. Could I simply overload std::endl to produce CR+LF, or is this a dumb idea for maintainability?

NB: I checked out this question, but it's asking about going the other way, and the accepted answer seems rather incomplete.

like image 776
J. Polfer Avatar asked Oct 30 '09 19:10

J. Polfer


1 Answers

Windows Notepad is pretty much the only Windows program you'll find that doesn't handle LF-only files properly. Almost everything else (including WordPad) handles LF-only files just fine.

This problem is a bug in Notepad.

like image 111
Greg Hewgill Avatar answered Sep 24 '22 03:09

Greg Hewgill