Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Write to file

Reply
Thread Tools

Write to file

 
 
T. A.
Guest
Posts: n/a
 
      08-17-2010
Hi,

I'm trying to write to a new file, wich I'm creating by

file=File.new('file.txt', 'w')

This works fine so far, the file is created in my working folder and
contains the text I'm putting in.

Now I want the user to input the filename, so I modified my source code
like this:


print "Please insert file name: "

file_name=gets

file=File.new(file_name+'.txt', 'w')

Running the Programm and inputting 'test' (without the ''), I get the
following Error:

..\rubyfile.rb:116:in `initialize': Invalid argument - test
(Errno::EINVAL)
txt
from ..\rubyfile.rb:116:in `new'
from ..\rubyfile.rb:116

I have no idea, what's going wrong... Since file_name is a string,
file_name+'.txt' ought to be a string too, and thus a valid argument for
the initializer method of File, or not?

Sorry, if my question is dumb, I'm a total noob with ruby...

Thanks for any help!

taura
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      08-17-2010
On Tue, Aug 17, 2010 at 12:46 PM, T. A. <tigrib85-> wrote:
> Hi,
>
> I'm trying to write to a new file, wich I'm creating by
>
> file=3DFile.new('file.txt', 'w')
>
> This works fine so far, the file is created in my working folder and
> contains the text I'm putting in.
>
> Now I want the user to input the filename, so I modified my source code
> like this:
>
>
> print "Please insert file name: "
>
> file_name=3Dgets
>
> file=3DFile.new(file_name+'.txt', 'w')
>
> Running the Programm and inputting 'test' (without the ''), I get the
> following Error:
>
> ..\rubyfile.rb:116:in `initialize': Invalid argument - test
> (Errno::EINVAL)
> .txt
> =A0 =A0 =A0 =A0from ..\rubyfile.rb:116:in `new'
> =A0 =A0 =A0 =A0from ..\rubyfile.rb:116
>


The problem is that gets returns the \n at the end of the string, so
file_name + ".txt" is "test\n.txt", which seems to be an invalid
filename. Try this:

file_name =3D gets.chomp


> I have no idea, what's going wrong... Since file_name is a string,
> file_name+'.txt' ought to be a string too, and thus a valid argument for
> the initializer method of File, or not?


BTW, use the block form of File.open, it ensures that the file is closed:

File.open("#{file_name}.txt", "w") do |file|
...
end

Jesus.

 
Reply With Quote
 
 
 
 
Taura A.
Guest
Posts: n/a
 
      08-17-2010
Hey, thanks very much, its working!

taura

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Program to open a file in binary, skip X bytes and write the rest ofthe file to a new file scad C++ 4 05-28-2009 08:47 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
is better to open, write, close file than open, write, append, close? Iulian Ilea Javascript 1 12-21-2006 04:32 PM
How to use Response.write to write to a specific area on a aspx pa =?Utf-8?B?QWJlbCBDaGFu?= ASP .Net 6 05-03-2006 10:16 PM



Advertisments