# Can't seem to write to a file properly

**URL:** <https://www.ruby-forum.com/t/cant-seem-to-write-to-a-file-properly/75427>\
**Category:** Ruby\
**Created:** [November 9, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/cant-seem-to-write-to-a-file-properly/75427 "2006-11-09T20:40:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [November 9, 2006, 8:40pm UTC](https://www.ruby-forum.com/t/cant-seem-to-write-to-a-file-properly/75427/1 "2006-11-09T20:40:19Z")

</div>

Hello,  
Can someone help me diagnose what’s going on with my script? Part of it  
is below. I need to open a file, read its contents, do something to its  
contents if a condition is true, then write new data to the end of the  
contents, of the open file. It’s doing it. It does add what I want it to  
add, but, it adds it hundreds and hundreds of times, not just one time,  
which is all I want.  
…  
Dir.glob("_.ps").each do |$psfile|  
$filetime = File.ctime($psfile)  
$filetime = $filetime.to\_s.gsub!(/ -._$/, “”)  
file\_contents = File.read($psfile)  
file\_contents.scan(/%%Pages: (\d{1,5})[]+\n/) do #look for  
pagecount in file  
totalpages = $1 #put that count in  
variable  
end

if (totalpages.to\_i % 2) !=0 then #if odd, then add  
blank pg  
totalpages = totalpages.to\_i + 1  
file\_contents.gsub!(/.\*$/, “%%Blank page for Asura.\n%%Page:  
#{totalpages.to\_i}\nshowpage\n”)  
end  
File.open("#{$psfile}", “a”) { |f| f.print file\_contents }  
end  
…

Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [November 9, 2006, 8:56pm UTC](https://www.ruby-forum.com/t/cant-seem-to-write-to-a-file-properly/75427/2 "2006-11-09T20:56:13Z")

</div>

On 11/9/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,  
> Can someone help me diagnose what’s going on with my script?  
> …  
> it adds it hundreds and hundreds of times, not just one time,  
> which is all I want.  
> …

I think the problem is probably this line:  
file\_contents.gsub!(/.\*$/, “%%Blank page for Asura.\n%%Page:  
#{totalpages.to\_i}\nshowpage\n”)  
You are replacing evevery single line in file\_contents with the  
“blank page” string  
So you will add as many strings as you have lines in the original file.

If I understand what you are asking, I don’t think you need the gsub  
at all. If you want to append that string to the end, just do:

File.open(“#{$psfile}”, “a”) { |f| f.print “%%Blank page for  
Asura.\n%%Page:  
#{totalpages.to\_i}\nshowpage\n”}

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [November 9, 2006, 9:15pm UTC](https://www.ruby-forum.com/t/cant-seem-to-write-to-a-file-properly/75427/3 "2006-11-09T21:15:38Z")

</div>

Adam S. wrote:

> On 11/9/06, Peter B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hello,  
> > Can someone help me diagnose what’s going on with my script?  
> > …  
> > it adds it hundreds and hundreds of times, not just one time,  
> > which is all I want.  
> > …
> 
> I think the problem is probably this line:  
> file\_contents.gsub!(/.\*$/, “%%Blank page for Asura.\n%%Page:  
> #{totalpages.to\_i}\nshowpage\n”)  
> You are replacing evevery single line in file\_contents with the  
> “blank page” string  
> So you will add as many strings as you have lines in the original file.
> 
> If I understand what you are asking, I don’t think you need the gsub  
> at all. If you want to append that string to the end, just do:
> 
> File.open(“#{$psfile}”, “a”) { |f| f.print “%%Blank page for  
> Asura.\n%%Page:  
> #{totalpages.to\_i}\nshowpage\n”}

Great. Thanks, Adam. As often happens, I was making my life complicated.  
I was using gsub! because someone suggested it instead of \<\<, which I  
had been using. But, this is much better. Thanks again.
