|
Posted by Richard Lynch on 04/07/05 05:48
On Wed, April 6, 2005 10:07 am, Saswat Praharaj said:
> I need some help in searching string in a file.
>
> My requirement is to search the string,append something to the string .
> Write the string back to the file without changing other parts of the
> file.
>
> e.g
> Suppose I have the following string in a file
>
>
> Set-Cookie: T_COOKIE=127.0.0.1-saswat-9; path=/
>
>
> The fixed string is "Set-Cookie: T_COOKIE="and rest of the string is
> variable.
> I would like to change the string "127.0.0.1-saswat-9" to say
> "127.0.0.1-saswat-9-1234"
Problem is, that's *MORE* bytes than are already there.
You can't just smush a few more bytes in.
You will *HAVE* to write out a new file.
But you can read in only the first couple hundred bytes until you find
your Set-Cookie: T_COOKIE, and then you write out the stuff before that,
write out your new data, then read/write the whole rest of the file.
You may also want to consider using, say, awk or sed or whatever from
http://php.net/exec -- I don't expect them to be that much faster than
PHP, but what do I know?
> My constraint is : I don't want to read the whole file into a buffer
> and work on that buffer as the file can be very large , sometimes more
> than 10 mb.
>
> My advantage is : I know that I will get the search string in first
> 200 bytes of the file , so I just need to read 200 bytes,play with
> that and write it back (overwriting the first 200 bytes with new 200
> some bytes) ..
If it's really 200 bytes for 200 bytes, yeah, fopen(..., 'r+') will work.
But if you can't squeeze your bytes into the same space as is already
there, you can't do that.
You might also be able, for the future, to start using:
127.0.0.1-saswat-9-0000000 and then you'd be able to replace up to
99999999 and get back something you can easily read/write/format to be
meaningful.
--
Like Music?
http://l-i-e.com/artists.htm
[Back to original message]
|