Sample C++ program:
#include <iostream>
#include <fstream>
int main(int argc, char* argv[]) {
if (argc > 1) {
std::ofstream output(argv[1]);
output << "Line 1\nLine 2\n" << std::flush;
} else {
std::cout << "Line 1\nLine 2\n" << std::flush;
}
}
Direct output (Windows 7):
TestProgram.exe direct.txt
yields
Line 1[LF]
Line 2[LF]
Redirected output:
Testprogram.exe >redir.txt
yields
Line 1[CR][LF]
Line 2[CR][LF]
- Where are the line endings being overwritten?
cout
? Console? Somewhere in between? And why? - Is there anything I can do about it? I've tried setting the
binary
flag onstd::cout
but that didn't change anything.
ETA: Using echo+redirection I can send a LF character to a file, using
echo line 1◙line 2 >echo.txt
(where the LF character is made with Alt-10 on the keypad). So it seems to be a C++ problem.