Newbie Question About files

J

Jeff Singer

I am still fairly new to ruby, and have been working on a fairly small
project.
Ive looked through the online book on ruby central, Why's Poignant
guide, and the ruby-doc documentation on both File and IO, but I still
can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to
send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

The error I get no matter how I format the name of the file is:
project.rb:177:in `initialize': Invalid argument -
*whatever_I_just_typed* (Errno::EINVAL)

Does anyone have any Ideas/suggestions on how to get this to work.
 
T

ts

J> Does anyone have any Ideas/suggestions on how to get this to work.

What is your OS, windows ?

Try

filename = gets.chomp # remove \n at the end



Guy Decoux
 
J

Jeff Singer

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
ts wrote:
<blockquote cite="(e-mail address removed)"
type="cite">
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
</pre>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->
J&gt; Does anyone have any Ideas/suggestions on how to get this to work.

What is your OS, windows ?

Try

filename = gets.chomp # remove \n at the end



Guy Decoux





</pre>
</blockquote>
Yes, Im on windows.<br>
<br>
Wouldn't your code add binary data to a file (I already have the data),
instead of output it to a file?<br>
</body>
</html>
 
P

Pit Capitain

Jeff said:
The error I get no matter how I format the name of the file is:
project.rb:177:in `initialize': Invalid argument -
*whatever_I_just_typed* (Errno::EINVAL)

Hi Jeff,

"gets" returns the string you entered *including* the CR, try

p filename

alter line 176. If you remove the trailing CR with

filename = gets.chomp

it should work.

Regards,
Pit
 
J

Jeff Singer

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Jeff Singer wrote:
<blockquote cite="(e-mail address removed)" type="cite">
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
ts wrote:
<blockquote cite="(e-mail address removed)"
type="cite">
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<blockquote type="cite">
<pre wrap="">"J" == Jeff Singer <a
class="moz-txt-link-rfc2396E" href="mailto:[email protected]">&lt;[email protected]&gt;</a> writes:
</pre>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>
<pre wrap=""><!---->
J&gt; Does anyone have any Ideas/suggestions on how to get this to work.

What is your OS, windows ?

Try

filename = gets.chomp # remove \n at the end



Guy Decoux





</pre>
</blockquote>
Yes, Im on windows.<br>
<br>
Wouldn't your code add binary data to a file (I already have the data),
instead of output it to a file?<br>
</blockquote>
Im sorry, I wasn't even thinking, I just saw gets, and didn't think
about where it would go, thank you.<br>
</body>
</html>
 
R

Robert Klemme

Jeff Singer said:
I am still fairly new to ruby, and have been working on a fairly small
project.
Ive looked through the online book on ruby central, Why's Poignant
guide, and the ruby-doc documentation on both File and IO, but I still
can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to
send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

The error I get no matter how I format the name of the file is:
project.rb:177:in `initialize': Invalid argument -
*whatever_I_just_typed* (Errno::EINVAL)

Does anyone have any Ideas/suggestions on how to get this to work.

Additional notes to make your code a bit safer:

- use binary output, regardless of OS, for documentation and portability

- use the block form of File.open, for exception safety

- use write instead of print, to avoid any interpretation of the data to
print

So, assuming that @file contains an array of strings with maxlen 500
(these hold the binary data I assume), that would make:

filename = gets.chomp
File.open( filename, 'wb' ) do |aFile|
@file.each {|data| aFile.write data}
end

You can even make it a 1-liner:

File.open( gets.chomp, 'wb' ) {|aFile| @file.each {|data| aFile.write
data} }

:)

Kind regards

robert
 
A

Ara.T.Howard

I am still fairly new to ruby, and have been working on a fairly small
project.
Ive looked through the online book on ruby central, Why's Poignant
guide, and the ruby-doc documentation on both File and IO, but I still
can't get the program to work.
Im trying ouput an array containing binary data to a file, the code is:

176: filename = gets
177: aFile = File.new(filename, 'w')
178: @file.each {|element| aFile.print element}
179: aFile.close

@file is an array of bytes, broken into blocks of 500 that Id like to
send to the file that Im prompted for.
And those numbers aren't actually there, as they are the line numbers.

guess you figured out the 'gets.chomp' bit (though i'd actually do gets.strip
since a leading space would hose you too).

assuming you meant 500 __bytes__ you can output the entire array at once
using:

File::new(filename, 'wb'){|f| f.write(@file.join.pack)}

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 
A

Ara.T.Howard

guess you figured out the 'gets.chomp' bit (though i'd actually do gets.strip
since a leading space would hose you too).

assuming you meant 500 __bytes__ you can output the entire array at once
using:

File::new(filename, 'wb'){|f| f.write(@file.join.pack)}

oops - hit sent before this was done... meant

File::new(filename,'wb'){|f| f.write @file.join}

sorry for noise.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| renunciation is not getting rid of the things of this world, but accepting
| that they pass away. --aitken roshi
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,260
Latest member
kentcasinohelpWilhemina

Latest Threads

Top