[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to always write Windows style newlines to a file?

Wes Gamble

12/13/2006 5:29:00 PM

I need to write a CSV file and I know that I always want Windows style
newlines on it (for import into Excel).

I want to make sure that every line is terminated with "\r\n" (CRLF)
regardless of platform. How do I write the "\n" when the code executes
on Windows without ending up with \r\r\n at the end of every line?

I've tried using <<, write, and syswrite against my IO (file) object.
How do I just write the CR and LF characters to my file?

Thanks,
Wes

--
Posted via http://www.ruby-....

7 Answers

Wes Gamble

12/13/2006 5:32:00 PM

0

I've done this in the past - is this the best way to do it?

#Add a \r character on non-Windows platforms
upload_file << "#{target.dump_output}"
upload_file << "\r" unless Config::CONFIG['target_os'] =~ /win/
upload_file << "\n"


--
Posted via http://www.ruby-....

Daniel Berger

12/13/2006 6:05:00 PM

0


Wes Gamble wrote:
> I need to write a CSV file and I know that I always want Windows style
> newlines on it (for import into Excel).

The line endings shouldn't matter. I write csv files on Linux systems
all the time, and open them with Excel without issue.

> I want to make sure that every line is terminated with "\r\n" (CRLF)
> regardless of platform. How do I write the "\n" when the code executes
> on Windows without ending up with \r\r\n at the end of every line?

If you still really want to do this, you can set $/ (record separator)
to "\r\n".

Regards,

Dan

Robert Klemme

12/13/2006 10:07:00 PM

0

On 13.12.2006 19:04, Daniel Berger wrote:
> Wes Gamble wrote:
>> I need to write a CSV file and I know that I always want Windows style
>> newlines on it (for import into Excel).
>
> The line endings shouldn't matter. I write csv files on Linux systems
> all the time, and open them with Excel without issue.
>
>> I want to make sure that every line is terminated with "\r\n" (CRLF)
>> regardless of platform. How do I write the "\n" when the code executes
>> on Windows without ending up with \r\r\n at the end of every line?
>
> If you still really want to do this, you can set $/ (record separator)
> to "\r\n".

You sure this works?

$ ruby -e '$/="\r\n"; puts "foo", "bar"' | od -c
0000000 f o o \n b a r \n
0000010

Output record separator doesn't seem to help either:

$ ruby -e '$\="\r\n"; puts "foo", "bar"' | od -c
0000000 f o o \n b a r \n
0000010

Explicit works:

$ ruby -e 'print "foo\r\n", "bar"' | od -c
0000000 f o o \r \n b a r
0000010

$ ruby -v
ruby 1.8.5 (2006-08-25) [i386-cygwin]

Hm... Is this a bug in Ruby 1.8.5 or am I missing something?

robert

Austin Ziegler

12/13/2006 10:15:00 PM

0

On 12/13/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> $ ruby -e 'print "foo\r\n", "bar"' | od -c
> 0000000 f o o \r \n b a r
> 0000010
>
> $ ruby -v
> ruby 1.8.5 (2006-08-25) [i386-cygwin]
>
> Hm... Is this a bug in Ruby 1.8.5 or am I missing something?

Yeah. You're using the disaster known as cygwin.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Robert Klemme

12/14/2006 9:31:00 AM

0

On 13.12.2006 23:14, Austin Ziegler wrote:
> On 12/13/06, Robert Klemme <shortcutter@googlemail.com> wrote:
>> $ ruby -e 'print "foo\r\n", "bar"' | od -c
>> 0000000 f o o \r \n b a r
>> 0000010
>>
>> $ ruby -v
>> ruby 1.8.5 (2006-08-25) [i386-cygwin]
>>
>> Hm... Is this a bug in Ruby 1.8.5 or am I missing something?
>
> Yeah. You're using the disaster known as cygwin.

So far it has served me very well. The fact that cygwin not generally
prevents the output of "\r\n" makes me believe there has to be something
in the implementation of Ruby. Maybe you can elaborate further in what
disastrous ways cygwin interferes here (other than running on a Windows
box). I also noticed this - on a Linux box:

PDBRK_MYSQL:~# ruby -e '$\="\r\n"; puts "foo", "bar"' | od -c
0000000 f o o \n b a r \n
0000010
PDBRK_MYSQL:~# uname -a
Linux PDBRK_MYSQL 2.6.8-2-386 #1 Tue Aug 16 12:46:35 UTC 2005 i686 GNU/Linux
PDBRK_MYSQL:~# ruby -v
ruby 1.8.2 (2005-04-11) [i386-linux]

Now, is Debian Linux disastrous, too? :-)

Kind regards

robert

Xavier Noria

12/14/2006 11:15:00 AM

0

On Dec 13, 2006, at 7:10 PM, Daniel Berger wrote:

> If you still really want to do this, you can set $/ (record separator)

That would be $\, the _output_ record separator.

> to "\r\n".

But that's a valid solution only if you set binmode on the
filehandle, otherwise you'll get a double \r on Windows.

-- fxn




Wes Gamble

12/14/2006 4:13:00 PM

0

So if I open the file using "b" I can write the bytes as I intend using
<<, is that correct?

That's simple and makes sense - how novel ;).

Thanks,
Wes


--
Posted via http://www.ruby-....