Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: What is the point of a+ and w+ when opening files?

by Marshall (Canon)
on May 18, 2023 at 00:12 UTC ( [id://11152273]=note: print w/replies, xml ) Need Help??


in reply to What is the point of a+ and w+ when opening files?

If you ever do use a r/w file, be aware that when you change modes (between write and read), an intervening seek is required to cause the buffers to be flushed to disk. You will see "seek 0 bytes from current position" in the code, that is a no-op functionally but causes a buffer flush.

A weird kind of file that you probably won't see is called a "sparse file". But this weird thing sometimes has its uses. I have made these both on Unix and on Windows. A seek doesn't necessarily leave physical blank space. If you start at the beginning if a file, write a few bytes, seek 1 GB, write a few bytes, seek 1 GB more, write a few bytes, the file on the disk will typically take about 16 KBytes (2 allocation units), not 2 GB. However, if you try to "cat" that file, it will be 2 GB long because the zeroes will be added. One of the big problems with a file like this is how to back it up! That is because a normal copy will "fill in the holes". Anyway, there are applications for a Huge address space that is only sparsely populated.

  • Comment on Re: What is the point of a+ and w+ when opening files?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11152273]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-23 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found