By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Q: Why are there file access modes?

P: n/a
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!
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Jakob Bieling" <ne*****@gmy.net> wrote...
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?
Jul 22 '05 #2

P: n/a
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:AU6zb.396577$HS4.3207826@attbi_s01...
"Jakob Bieling" <ne*****@gmy.net> wrote...
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?


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
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #3

P: n/a
Jakob Bieling wrote:
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #4

P: n/a
Andrey Tarasevich wrote:
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.


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

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #5

P: n/a
Victor Bazarov wrote:
"Jakob Bieling" <ne*****@gmy.net> wrote...
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.


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.

Jul 22 '05 #6

P: n/a

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bq*************@news.t-online.com...
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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.