Q: Why are there file access modes?

J

Jakob Bieling

Hi,

I was wondering why there are file access modes, which you have to
specify when opening the file. I am specifically talking about the 'read',
'read-write' and 'write' distinction. As far as I can tell, it only makes
sure, you (the programmer) do not accidently write to it when you opened it
for reading. To me, this seems like another one of those things that just
make life harder.

I am asking this, because I am writing a wrapper (not *just* a wrapper,
tho) for file operations with files on a hard disk. Should I provide those
different flags as well? If so, what for? Why don't I just let the user of
the code 'open' the file and read and write to it as she wishes?

Thanks!
 
V

Victor Bazarov

Jakob Bieling said:
I was wondering why there are file access modes, which you have to
specify when opening the file. I am specifically talking about the 'read',
'read-write' and 'write' distinction. As far as I can tell, it only makes
sure, you (the programmer) do not accidently write to it when you opened it
for reading. To me, this seems like another one of those things that just
make life harder.

AFAIK, there are platforms that allow to simultaneously open the same
file for reading by more than one process. Simultaneous writing is
not allowed on those systems. So, if you open for write or read-write
a file that is being read by another process, the operation will fail.

Since those OS traits are not standardised, C++ cannot make any real
claims about it, but it provides some kind of mechanism that would be
functioning if such abilities exist. Basically, the same as with text
and binary modes. For Unices it makes no difference, for DOS and Win
it does.
I am asking this, because I am writing a wrapper (not *just* a wrapper,
tho) for file operations with files on a hard disk. Should I provide those
different flags as well?
Certainly.

If so, what for?

For flexibility.
Why don't I just let the user of
the code 'open' the file and read and write to it as she wishes?

Because it may not be what the user wants. Why castrate functionality
when it's not that difficult to simply delegate it?
 
J

Jakob Bieling

Victor Bazarov said:
AFAIK, there are platforms that allow to simultaneously open the same
file for reading by more than one process. Simultaneous writing is
not allowed on those systems. So, if you open for write or read-write
a file that is being read by another process, the operation will fail.

Since those OS traits are not standardised, C++ cannot make any real
claims about it, but it provides some kind of mechanism that would be
functioning if such abilities exist. Basically, the same as with text
and binary modes. For Unices it makes no difference, for DOS and Win
it does.


For flexibility.


Because it may not be what the user wants. Why castrate functionality
when it's not that difficult to simply delegate it?

Hi Victor,

thanks a lot for your quick reply! I guess you come to ask those kind of
questions when you only live in your little Windows world ;) Looks like
I'll go thru the little work and provide the distiction when opening.

Regards
 
A

Andrey Tarasevich

Jakob said:
I was wondering why there are file access modes, which you have to
specify when opening the file. I am specifically talking about the 'read',
'read-write' and 'write' distinction. As far as I can tell, it only makes
sure, you (the programmer) do not accidently write to it when you opened it
for reading. To me, this seems like another one of those things that just
make life harder.

I am asking this, because I am writing a wrapper (not *just* a wrapper,
tho) for file operations with files on a hard disk. Should I provide those
different flags as well? If so, what for? Why don't I just let the user of
the code 'open' the file and read and write to it as she wishes?
...

In addition to Victor's reply I'd like to add that there are other
reasons to specify these modes when opening a file. For example, in many
cases on many platforms file I/O is cashed in memory. The cashing can be
organized more efficiently if it is known that the file will be used for
reading (or writing) only.
 
A

Andrey Tarasevich

Andrey said:
In addition to Victor's reply I'd like to add that there are other
reasons to specify these modes when opening a file. For example, in many
cases on many platforms file I/O is cashed in memory. The cashing can be
organized more efficiently if it is known that the file will be used for
reading (or writing) only.

I meant "cached", not "cashed" :)
 
R

Rolf Magnus

Victor said:
AFAIK, there are platforms that allow to simultaneously open the same
file for reading by more than one process. Simultaneous writing is
not allowed on those systems. So, if you open for write or read-write
a file that is being read by another process, the operation will fail.

And further more, some operating systems can give users different rights
for reading and writing files, so they are allowed to write only to
files that they created themselves or that they are explicitly allowed
to write to, but are allowed to read many other files. In fact, those
access restrictions belong to the most basic and important features of
many modern OSs.
 
J

jeffc

Jakob Bieling said:
Hi,

I was wondering why there are file access modes, which you have to
specify when opening the file. I am specifically talking about the 'read',
'read-write' and 'write' distinction. As far as I can tell, it only makes
sure, you (the programmer) do not accidently write to it when you opened it
for reading. To me, this seems like another one of those things that just
make life harder.

No, usually it's so other code doesn't accidentally write to it. When
someone else tries to open that file, they might want exclusive access, or
not want to mess with it while someone else has write access to it instead
of just read access. That could really screw up certain operations. Also,
some files are read only. That protects the file itself from a program
opening it with write access.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top