# Copy txt file - only copies 1/3 of the file - what happened?

**URL:** <https://www.ruby-forum.com/t/copy-txt-file-only-copies-1-3-of-the-file-what-happened/155370>\
**Category:** Ruby\
**Created:** [December 11, 2008, 4:42am UTC](https://www.ruby-forum.com/t/copy-txt-file-only-copies-1-3-of-the-file-what-happened/155370 "2008-12-11T04:42:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 4:42am UTC](https://www.ruby-forum.com/t/copy-txt-file-only-copies-1-3-of-the-file-what-happened/155370/1 "2008-12-11T04:42:38Z")

</div>

File.open(‘oldFile.txt’, ‘r’) do |f1| #file contains XML type data  
m = f1.gets  
File.open(‘newFile.txt’,‘w’) do |f2| #newFile to separate data in  
oldFile  
f2.puts m.split("\<")  
end  
end

Hello All.  
There is something that I did that was weird here. I say this because  
when I open the new file that it creates its 1/3 the size of the  
original. It seems to just completely cut off. I took off the .split and  
it still did this, so I know it has nothing to do with split. What do  
you think is going on?

oldFile.txt  
contains XML formatted data.

newFile.txt  
is supposed to break each line up what starts with \< giving a new line  
per element for later use.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 4:47am UTC](https://www.ruby-forum.com/t/copy-txt-file-only-copies-1-3-of-the-file-what-happened/155370/2 "2008-12-11T04:47:40Z")

</div>

On Dec 10, 2008, at 9:37 PM, Mmcolli00 Mom wrote:

> File.open(‘oldFile.txt’, ‘r’) do |f1| #file contains XML type data  
> m = f1.gets  
> File.open(‘newFile.txt’,‘w’) do |f2| #newFile to separate data in  
> oldFile  
> f2.puts m.split("\<")  
> end  
> end

Perhaps replace line 2 with:

```
 m = f1.read

```

?

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 5:02am UTC](https://www.ruby-forum.com/t/copy-txt-file-only-copies-1-3-of-the-file-what-happened/155370/3 "2008-12-11T05:02:53Z")

</div>

On Thu, Dec 11, 2008 at 11:37 AM, Mmcolli00 Mom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> File.open(‘oldFile.txt’, ‘r’) do |f1| #file contains XML type data  
> m = f1.gets

ok, this gets just one line of text fr f1

> ```
> File.open('newFile.txt','w') do |f2| #newFile to separate data in
> 
> ```
> 
> oldFile  
> f2.puts m.split(“\<”)

and you placed that one line on f2

> end  
> end

and that is all. you just copied one line.

hint: File.open by itself does not loop.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 11, 2008, 5:10am UTC](https://www.ruby-forum.com/t/copy-txt-file-only-copies-1-3-of-the-file-what-happened/155370/4 "2008-12-11T05:10:58Z")

</div>

Thanks for the help Matthew and Guest botp! It worked great.
