Reputation: 549
I'm learning perl and writing a script to output all contents in the directory tree of the script to a file. The problem is in the text file the entire output is on one line even though I'm including "\n". I have the save code printing to the console and I get the desired results there, which is one entry per line.
#!/usr/bin/perl -w
use Cwd;
use File::Find;
use strict;
my $file = "temp.txt";
my $dir = getcwd;
unless ( open FILE, '>>' . $file ) {
die "\nUnable to create $file\n";
}
find( \&print_type, $dir );
sub print_type {
if (-f) {
print FILE "File: " . "$_\n";
print "File: " . "$_\n";
}
if (-d) {
print FILE "Directory: " . $_ . "\n";
print "Directory: " . $_ . "\n";
}
}
close FILE;
I also tried putting a \n before the "Directory : " and that didn't work either.
Upvotes: 1
Views: 16462
Reputation: 226
open the file in APPEND mode or WRITE mode,and use the opened file descriptor for writting and appending data to the file, using PRINT stantment in perl. IN Linux only the "\n" are considered,not for WINDOW users,so Linux users append a extra character at every line end called NEWLINE "\n";
Upvotes: 1
Reputation: 549
Turns out the code works just fine, but I using Windows notepad to read my output file and it doesn't recognize "\n", I just needed to open it up in just about any other text editor to see that (notepad++, wordpad, emacs).
It may be that notepad could have worked if I weren't using Cygwin.
Upvotes: 2