473,378 Members | 1,309 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,378 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 16864
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.