From: Konrad Meyer Date: 2007-08-10T18:14:18+09:00 Subject: Re: newbie question: how to seek in a file --nextPart1908127.qq0SpZucDl Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On Friday 10 August 2007 02:06:53 am Lutz Horn wrote: > Hi, >=20 > Ak 756 wrote: > > I want to find a string in a text file, then read some bytes data after > > the position of this string. Would anybody kindly help to tell me how to > > do? >=20 > You could do something like this: >=20 > File.open("my-file.txt") do |f| > i =3D f.read.index(/search/) > f.seek(i) > puts f.read(10) > end At that point though, you've already read the entire contents of the file into memory (f.read()) so there's no point going back and seeking in the file when: * Seeking from memory should always be faster than from disk * You're already ignoring the memory issues for large files =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart1908127.qq0SpZucDl Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBGvCxuCHB0oCiR2cwRApWaAJ96xfNzAdpus95l1wxryexAnsDA4QCeK0DX Oc8sMYHAHTcvY17dAKhRFJk= =lia6 -----END PGP SIGNATURE----- --nextPart1908127.qq0SpZucDl--