Home:ALL Converter>newlines when displayed in Windows

newlines when displayed in Windows

Ask Time:2016-02-08T12:08:57         Author:CJ7

Json Formatter

open OUT, ">output.txt";
print OUT "Hello\nWorld";

When I run the above perl code in a Unix system and then transfer output.txt to Windows and open it in Notepad it shows as:

HelloWorld

What do I need to do to get the newlines displaying properly in Windows?

Author:CJ7,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35262218/newlines-when-displayed-in-windows
Borodin :

Text file line endings are platform-specific. If you're creating a file intended for the Windows platform then you should use\n\nopen OUT, '>:crlf', 'output.txt' or die $!;\n\n\nThen you can just\n\nprint OUT \"Hello\\nworld!\\n\";\n\n\nas normal\n\nThe :crlf PerlIO layer is the default for Perl executables built for Windows, so you don't need to add it to code that will create files for its intended platform. For portable software you can check the host system by examining the built-in variable $^O",
2016-02-08T06:36:06
kizeloo :

Windows uses carriage-return + linefeed:\n\nprint OUT \"Hello\\r\\nWorld\";\n",
2016-02-08T04:16:27
stevieb :

I wrote the File::Edit::Portable module that eliminates these problems. Although you can use it to write (along with many other things), you only need the read() functionality in this case.\n\nInstall the module, and at the top of your script, add:\n\nuse File::Edit::Portable;\n\n\nWhen opening/reading the file, you can just do:\n\nmy $rw = File::Edit::Portable->new;\n\nmy $fh = $rw->read('file.txt');\n\n\nNo matter what the line endings are or what platform you're on, it does all of the cross-platform work in the background so you don't have to. That way, you can open the file on any system, regardless of what line endings you've decided to use, and it just works.",
2016-02-08T14:56:11
yy