[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby File#sysread

Aatch

6/15/2007 2:08:00 AM

I am attempting to create a program that can transfer files. It so far
can send pure-text files no problem, but it has issues with reading
anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.

After a bit of fiddling around I found that the program wasn't reading
the file properly. I'm currently using File#sysread to read the file and
File#syswrite to write the file. I don't know why it wont read the whole
file, can some body please help. Here is a copy of a similar program
with the same problem. (please dont mock it uselessness, it was made to
try and sort aforementioned problem).


f1 = File.new("BIG.txt")
f2 = File.new("big.txt", "w")

contents = f1.sysread(f1.stat.size-2)

print f1.stat.size.to_s + "\n"
print contents.length.to_s + "\n"

f2.syswrite contents

print f2.stat.size.to_s + "\n"

sleep 2

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

5 Answers

Tim Hunter

6/15/2007 2:21:00 AM

0

Aatch wrote:
> I am attempting to create a program that can transfer files. It so far
> can send pure-text files no problem, but it has issues with reading
> anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.
>

On Windows? "rb"

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Aatch

6/15/2007 3:53:00 AM

0

Tim Hunter wrote:
> Aatch wrote:
>> I am attempting to create a program that can transfer files. It so far
>> can send pure-text files no problem, but it has issues with reading
>> anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.
>>
>
> On Windows? "rb"

<.< >.> yes. I have no other choice. But yes, I was thinking it might be
windows fault

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

Bill Kelly

6/15/2007 4:06:00 AM

0

From: "James Milner" <bladeon@gmail.com>
> Tim Hunter wrote:
>> Aatch wrote:
>>> I am attempting to create a program that can transfer files. It so far
>>> can send pure-text files no problem, but it has issues with reading
>>> anything else. EG. It cut a 6 MB mp3 down to a 6 kb file.
>>>
>>
>> On Windows? "rb"
>
> <.< >.> yes. I have no other choice. But yes, I was thinking it might be
> windows fault

Just in case the answer wasn't clear, Windows pretends that "text" files
are different from "binary" files.

So you'll want to use "binary mode" to read/write binary files on Windows.

Thus, use "rb" instead of "r" to read a binary file, and "wb" instead of "w"
to write a binary file.


Hope this helps,

Bill



Aatch

6/15/2007 4:10:00 AM

0

Bill Kelly wrote:
> From: "James Milner" <bladeon@gmail.com>
>> windows fault
> Just in case the answer wasn't clear, Windows pretends that "text" files
> are different from "binary" files.
>
> So you'll want to use "binary mode" to read/write binary files on
> Windows.
>
> Thus, use "rb" instead of "r" to read a binary file, and "wb" instead of
> "w"
> to write a binary file.
>
>
> Hope this helps,
>
> Bill

Ahh, thanks. I've had little experience with the File class in ruby.
I'll test it now. It should ::prays:: work.

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

Nobuyoshi Nakada

6/17/2007 8:35:00 AM

0

Hi,

At Fri, 15 Jun 2007 13:09:49 +0900,
Aatch Random wrote in [ruby-talk:255684]:
> I'll test it now. It should ::prays:: work.

It's an eternal curse against Windows programers. :)

--
Nobu Nakada