| | |
Problems writing stdout to a file, please help!
Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.
open STDOUT,"> ${output}" || die "$0 can't open $ifile";
This is how I'm printing:
print $_;
I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
open STDOUT,"> ${output}" || die "$0 can't open $ifile";
This is how I'm printing:
print $_;
I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
Have you defined one of the following variables in your script anywhere:
Because, that "^M" is the "\r" from a "\r\n" windows/dos style line ending rather than the standard "\n" of the unix/linux line ending (at least that is what I have always assumed it to be, since that is what I see when I copy a text file in binary mode in ftp from dos to unix). So, if running the code on a Linux machine produces those in the output file, I can only guess that you have defined one of the above variables to be "\r\n".
Perl Syntax (Toggle Plain Text)
IO::Handle->output_record_separator EXPR $OUTPUT_RECORD_SEPARATOR $ORS $\
Because, that "^M" is the "\r" from a "\r\n" windows/dos style line ending rather than the standard "\n" of the unix/linux line ending (at least that is what I have always assumed it to be, since that is what I see when I copy a text file in binary mode in ftp from dos to unix). So, if running the code on a Linux machine produces those in the output file, I can only guess that you have defined one of the above variables to be "\r\n".
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Sep 2007
Posts: 176
Reputation:
Solved Threads: 20
•
•
•
•
I'm having problems writing records to an output file. When I do it in Textpad running on Windows, the output file looks fine. When, however, I copy the script to a Linux machine and use the exact same code, it appends a ^M character on each line written to the file. I have no idea why this is happening. I'm writing to the file by redirecting STDOUT to it.
open STDOUT,"> ${output}" || die "$0 can't open $ifile";
This is how I'm printing:
print $_;
I'm new to this, so if there's a better way to do it I'm all ears!
Thanks for the help!
Where is $ifile defined?
First you should be using 'strict' and 'warnings' in your scripts, if you aren't already. This will save you hours of debugging time:
Perl Syntax (Toggle Plain Text)
#! /usr/bin/perl use strict; use warnings;
To open a file the 3-argument style with lexical filehandles is now generally recommended:
Perl Syntax (Toggle Plain Text)
my $outfile = "output.txt"; open (my $FILE, '>', $outfile) or die "Can't open $outfile: $!\n"; #ALWAYS check the result of an open, and use $! to tell you what happened print $FILE "whatever\n"; close $FILE or die "Could not close $outfile: $!\n";
When I'm developing a script, I also put this block of code at the top of the script ..
Perl Syntax (Toggle Plain Text)
BEGIN { open (STDERR,">>$0-err.txt"); print STDERR "\n",scalar localtime,"\n"; }
It writes the output of errors to a file in the same directory as the script, using the script name + '-err.txt' as a file name. Much easier than digging through server log files. When the script is working, just comment out the block.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
"Others make web sites. We make web sites work!"
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- Writing data into a file (C)
- Writing data to a file. (C++)
- Reading or writing a double into a bin file (C++)
- Writing to binary file, erases previous data? (C++)
- problems writing multiple files (Perl)
- ofstream - detect if file has been deleted between open and close (C++)
- writing an array to a file (C++)
Other Threads in the Perl Forum
- Previous Thread: Web Scraping Help
- Next Thread: Problem with concatenation
| Thread Tools | Search this Thread |






