Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a vxworks OS. I'm reading the file using MFC on a windows XP PC. The file is stored on a Windows 2000 server. I tried using CStdioFile.Open using the CFile::modeRead flag. When did this i received a CFileException with the cause stating CFileException: :sharingViolati on SHARE.EXE was not loaded, or a shared region was locked. Then i opened the file with the CFile::modeRead | CFile::shareDen yNone. This allows me to open the file, but when i do a CStdioFile::Rea dString() I sometimes read the file just fine, other times I am unable to read anything (no data is received from the file). Any ideas or sujestions? I thought about using a different class to access the file.
6 17007
You cannot have someone reading a file while someone else is writing to it. The writer changes the file structure underneath the feet of the reader. This is called a race condition and it usually precedes a crash.
What you have to do is lock the file. That is, obtain a lock. A writer checks to see iof the lock is set. If so, it waits an tries again later. Ditto for the reader.
The easiest thing to do is to use a lock file. A writer tries to open the lock file and if it does not exists, creates the lock file. A reader then opens the lock file and because it opens, it means the data file is locked so the reader goes away or waits.
The writer deletes the lock file after the write operation is complete.
The reader needs to do the same thing. If the lock file does not open, the reader creates it and that causes a writer to understand the data file is locked so the writer waits and tries to open the lock file again.
Nepomuk 3,112
Recognized Expert Specialist
You cannot have someone reading a file while someone else is writing to it...
Little correction:
You cannot have someone reading a file while someone else is writing to it with Windows! With Linux (and I would guess Unixes as well - so that would probably include OS X too) it's possible. And, in some (ok, very few) cases, actually a good idea.
Greetings,
Nepomuk
Little correction:
You cannot have someone reading a file while someone else is writing to it with Windows! With Linux (and I would guess Unixes as well - so that would probably include OS X too) it's possible. And, in some (ok, very few) cases, actually a good idea.
So, while you are reading this tree another thread comes along and alters the structure of the tree or moves stuff around or deletes stuff making the reader use invalid addresses and this is OK?
i used a function like this on a fedora server last year, it i could open a text file and watch it have messages append to the end, updateing live. Im not sure how this function worked exactly but it is possible. I will try and digg up the modual and let you know when i get home.
Good luck
Nepomuk 3,112
Recognized Expert Specialist
So, while you are reading this tree another thread comes along and alters the structure of the tree or moves stuff around or deletes stuff making the reader use invalid addresses and this is OK?
First part: Yes!
Second part: I don't think so.
Greetings,
Nepomuk
Multiple readers are OK. However, the presence of one writer requires a lock. That is, the reader has to lock to prevent the writer from screwing up the read and the writer has to lock top prevent the reader from crashing the program.
Depending upon your file structure, you might be able to lock just a section of the file, maybe by locking a memory page, rather than the entire file.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Otik |
last post by:
Hi,
I'm trying to open a file with read access only, e.g. FileStream fs = new
FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is open
by another process and the FileStream constructor throws an IOException. I
thought that it was possible to open a file with read access even if it's
being used by another process. Is that not true?
Thanks,
Otik
|
by: Mountain Bikn' Guy |
last post by:
I am having serious problems with the following IDE bug:
Could not write to output file 'x.dll' -- 'The process cannot access the
file because it is being used by another process. '
and
BUG: "Could Not Copy Temporary Files to the Output Directory" Error Message
When You Build a Solution That Contains Multiple Projects
I have tried all the solutions in Microsoft Knowledge Base Article - 313512.
|
by: trellow |
last post by:
Hello,
I am writing an application that needs to read a file that is already open by another process for writing.
When I do the following:
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
I get the following exception:
|
by: Gabe Moothart |
last post by:
Hi,
I'm writing a windows service which interacts with a separate process.
Basically, it calls a process which creates a file, and then my service
reads that file. The problem is, the external process can take a second
or two to finish writing the file. If I try to read the file to soon, I
get an exception that "The process cannot access the file because it is
being used by another process".
I could just set a timer, but the time it...
|
by: UnnamedIdiot |
last post by:
There ought to be a simple way, but my searches are coming up void. . .
The situation is simple. I have a text log file which is open and being
written to by another application. I wish to monitor the file for
particular entries and respond programmaticaly. I cannot open the file
via streamreader, or I get the typical exception that the file is being
used by another process. (I know this.)
I can create a filesystemmonitor object...
| |
by: George |
last post by:
Hi,
I am re-writing part of my application using C#. This application starts
another process which execute a "legacy" program. This legacy program writes
to a log file and before it ends, it writes a specific string to the log file.
My original program (MKS Toolkit shell program) which keeps running "grep"
checking the "exit string" on the "log files". There are no file sharing
problem.
|
by: Tom Wright |
last post by:
I'm writing a program which reads a series of data files as they are dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.
I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for now
and come back to it later. This needs to work on linux and windows. Mac
OS would...
|
by: nightangel |
last post by:
Hi dude,what i was done in my application is uploading a image file to my server using FTP, it work great when pushing a file into the server path using FTP.
The problem i met now is i need to do a resuming for the file uploading if the client connection or server connection is down.
E.g: Sunset.jpg (500KB) when i upload until half of it, (332KB in the server) and unplug my connection, it will looping and try to connect to the server, after...
|
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= |
last post by:
Hi all
I am trying to loop a Textfile.
This Textfile is located on a shared drive and it is
used by another process.
When I try to read it I get an exeption:
The process cannot access the file 'file.log' because it is being used by
another process.
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |