473,782 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Q: Why are there file access modes?

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
6 2064
"Jakob Bieling" <ne*****@gmy.ne t> 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
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:AU6zb.3965 77$HS4.3207826@ attbi_s01...
"Jakob Bieling" <ne*****@gmy.ne t> 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
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
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
Victor Bazarov wrote:
"Jakob Bieling" <ne*****@gmy.ne t> 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

"Jakob Bieling" <ne*****@gmy.ne t> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2390
by: Karthikesh Raju | last post by:
Hi All, i am wondering about the best way to read in a configuration file that goes like: ########### source_dir = '/home/karthik/Projects/python' data_dir = '/home/karthik/Projects/data'
0
1335
by: pxlpluker | last post by:
What exactly does the underlined text mean. w & a obviously mean you are going to update the file. So what does the + mean on w+ & a+ mean? Also please could someone explain w+ truncating the file. I may just be dense so please be patient :) ------------------------------------------------------------------------- file( filename ] )
8
9857
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
6
3278
by: Tom | last post by:
I am having trouble when I read a file and another process is trying to update it. So I need a rountine to copy a file in asp.net. Can anyone assist? Thanks Tom
31
764
by: David Warner | last post by:
Greetings! I am working on a C app that needs to read print files generated by lp that contain HP LaserJet PCL codes. If the PCL contains binary data to define a bit map to be printed on the page, either icon or soft-font download, the EOF test ends prematurely and does not read the entire file. How do I get around testing for EOF and not detect the EOF value within the file? Am I missing something? More Data:
5
1306
by: erikcw | last post by:
Hi all, I've created a script that reads in a file, replaces some data (regex), then writes the new data back to the file. At first I was convinced that "w+" was the tool for the job. But now I'm finding that the contents of the file are deleted (so I can't read the data in). f = open('_i_defines.php', 'w+')
4
2052
by: HMS Surprise | last post by:
After reading a file is it possible to write to it without first closing it? I tried opening with 'rw' access and re-winding. This does not seem to work unless comments are removed. Also, does close force a flush? Thanks, jh
10
22750
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app) outFile.write("teststring", 10); outFile.close(); If I leave out the ios::ate and ios::app modes my string is written to the start of the file as I'd expect but I want to write the data to
6
5312
by: | last post by:
Hi, I need to get the the display monitor native resolution.I have tried SystemInformation.PrimaryMonitorSize. it is giving the exisiting resolution of the display monitor that is connected. Please help me out to find the way to get the native resolution of the monitor that is connected -BMK
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9942
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.