开发者

Shrinking file that is opened in read/write

开发者 https://www.devze.com 2023-01-26 16:17 出处:网络
In perl: I have a file opened in read/write, with an exclusive lock. open( $f, \"+< $filename\" ); flock( $f, LOCK_EX );

In perl:

I have a file opened in read/write, with an exclusive lock.

open( $f, "+< $filename" );

flock( $f, LOCK_EX );

If I write more data to the file than it previously held, the file will grow.

If I write less data, my new contents are at the beginning, but the old contents are still there at the end of the file.

This isn't surprising, however it's not what I want.

Is there a simple way to shrink the file while it is opened in read/write? Basically I want to tell it to end the file at exactly this byte position.

I know I can open it differently, and I'm considering doing that, but a one line fix wou开发者_开发知识库ld be nice.


truncate


I actually don't know about perl, but since ftruncate(2) would be the C function, maybe this helps?

0

精彩评论

暂无评论...
验证码 换一张
取 消