473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Reading file while another process is writing to it.

464 Expert 256MB
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::sharingViolation SHARE.EXE was not loaded, or a shared region was locked. Then i opened the file with the CFile::modeRead | CFile::shareDenyNone. This allows me to open the file, but when i do a CStdioFile::ReadString() 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.
Apr 18 '08 #1
6 16880
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 20 '08 #2
Nepomuk
3,112 Expert 2GB
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
Apr 21 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
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?
Apr 22 '08 #4
SpecialKay
109 100+
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
Apr 22 '08 #5
Nepomuk
3,112 Expert 2GB
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
Apr 22 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
4
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:...
3
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,...
8
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...
3
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...
13
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...
8
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...
0
nightangel
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
jinu1996
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...
0
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...
0
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,...
0
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...

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.