# Appending to file in arbitrary position

**URL:** <https://www.ruby-forum.com/t/appending-to-file-in-arbitrary-position/77387>\
**Category:** Ruby\
**Created:** [November 27, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/appending-to-file-in-arbitrary-position/77387 "2006-11-27T15:48:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![quixote\_arg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/278dde/32.png) [@quixote\_arg](https://www.ruby-forum.com/u/quixote_arg)\
**Post date:** [November 27, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/appending-to-file-in-arbitrary-position/77387/1 "2006-11-27T15:48:30Z")

</div>

Hi, I’m implementing the server side of a file upload with resume option  
part of a web app.

The client sends the last position so I have to append to the existing  
file from that position.

Current code is:

@uploadfile = @params[“uploadfile”]  
content\_range = @request.env[‘HTTP\_CONTENT\_RANGE’]  
filename = @uploadfile.original\_filename  
data = @uploadfile.read

# position to append from

pos = content\_range[/\d+/].to\_i

f = File.open("#{filename}", “a”)  
#f.pos = pos  
f.seek(pos)  
f.write(data)  
f.close

I’ve tried opening the file with a and a+ modes. I’ve tried doing  
rewing, setting pos (like the commented line) and seek, and none of them  
works, the new data is always appended at the end of the file.

Any clues?

Thanks

---

<div class="post-metadata">

**Author:** ![quixote\_arg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/278dde/32.png) [@quixote\_arg](https://www.ruby-forum.com/u/quixote_arg)\
**Post date:** [November 27, 2006, 4:07pm UTC](https://www.ruby-forum.com/t/appending-to-file-in-arbitrary-position/77387/2 "2006-11-27T16:07:52Z")

</div>

Tulsi D. wrote:

> filename = @uploadfile.original\_filename
> 
> I’ve tried opening the file with a and a+ modes. I’ve tried doing  
> rewing, setting pos (like the commented line) and seek, and none of them  
> works, the new data is always appended at the end of the file.

Open the file in r+ mode. This is read-write mode that starts at the  
beginning of the file and allows you to seek to a specific position.

Jamey

---

<div class="post-metadata">

**Author:** ![quixote\_arg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/278dde/32.png) [@quixote\_arg](https://www.ruby-forum.com/u/quixote_arg)\
**Post date:** [November 27, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/appending-to-file-in-arbitrary-position/77387/3 "2006-11-27T16:11:53Z")

</div>

Jamey C. wrote:

> Open the file in r+ mode. This is read-write mode that starts at the  
> beginning of the file and allows you to seek to a specific position.

I can’t believe it was so easy, I’ve never even read the “r” section,  
assuming it was read only… thanks a lot, it works great!

---

<div class="post-metadata">

**Author:** ![quixote\_arg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/278dde/32.png) [@quixote\_arg](https://www.ruby-forum.com/u/quixote_arg)\
**Post date:** [November 27, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/appending-to-file-in-arbitrary-position/77387/4 "2006-11-27T19:36:16Z")

</div>

On 11/27/06, Tulsi D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Jamey C. wrote:
> 
> > Open the file in r+ mode. This is read-write mode that starts at the  
> > beginning of the file and allows you to seek to a specific position.  
> > I can’t believe it was so easy, I’ve never even read the “r” section,  
> > assuming it was read only… thanks a lot, it works great!

If you want this to be cross-platform, make sure you actually open it  
“rb+” or your code will mysteriously fail on Windows.

-austin
