473,396 Members | 1,998 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,396 software developers and data experts.

Simultaneously Write to and Read from the same file

cnu
My program generates a log file for every event that happens in the program.
So, I open the file and keep it open till the end. This is how I open the
file for writing:

<CODE>
public CLogHandler()
{
this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append,
System.IO.FileAccess.Write, System.IO.FileShare.Read);
this.m_swLog = new StreamWriter(this.m_fsLog);
if(this.m_swLog != null) this.m_swLog.AutoFlush = true;
}

pubic WriteLogMessage()
{
if(this.m_swLog != null) this.m_swLog.Write("[" +
DateTime.Now.ToString("HH:mm:ss") + "] " + aMsg + Environment.NewLine);
}
</CODE>

One of the requirements is to display the current log file contents in a
child form. So, I open the log file for reading like this :

<CODE>
System.IO.FileStream fs = new FileStream(this.m_strCurrentFileName,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.StreamReader sr = new StreamReader(fs);

while(sr.Peek() > -1) this.DisplayLogText(sr.ReadLine() +
Environment.NewLine);

fs.Close();
</CODE>

As you see, when I open the file for writing, I'm granting FileShare.Read,
which means other processes can open the file for reading. But when I try to
open the file for reading, it gives IOException that the file is being used
by another process.

So, how can we write and read to and from a file simultaneously in the same
program (ofcourse, without stopping the write process)??

Oct 4 '05 #1
1 6707
cnu wrote:
this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append,
System.IO.FileAccess.Write, System.IO.FileShare.Read);
This part is OK. You are opening the file for writing
(FileAccess.Write) and you grant other processes the right to Read
(FileShare.Read).
System.IO.FileStream fs = new FileStream(this.m_strCurrentFileName,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
This is where the problem is. You open the file for reading
(FileAccess.Read) but then you try to restrict other processes by
saying they can only Read (FileShare.Read). FileShare.Read means:
"Only let other processes Read from this file". Since you have already
opened the file for writing elsewhere, this is causing a conflict. You
should specify FileShare.ReadWrite when opening the file for reading.

As you see, when I open the file for writing, I'm granting FileShare.Read,
which means other processes can open the file for reading. But when I try to


Right, but when you open it for reading, you specified FileShare.Read
which means other processes cannot write. Since your first process is
already writing, it can't open in this mode.

Oct 4 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: BasicQ | last post by:
I am running an executable from my aspx page with the click of a button. A date is passed as an argument. I am able to get the standardoutput from the Process(Exe) into the label of my page after...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: Tito | last post by:
For an internet telephone application, I need to be able to read and write data to and from /dev/dsp simultaneously. I wrote some code and its not working. Anyone have any working code to do...
1
by: Siegfried Heintze | last post by:
I have some cygwin cron jobs running under the Administrator account populating a MSAccess database that is simultaneously being queried by IIS/ASP.NET/C#. If I manually use windows explorer to...
8
by: Raghu | last post by:
Is it possible to write a new file and after writing few bytes, is it possible to read from it from another file stream while write continues? Is there another steam for this type of operation? ...
12
by: Sean Davis | last post by:
I am working on a simple script to read from one database (oracle) and write to another (postgresql). I retrieve the data from oracle in chunks and drop the data to postgresql continuously. The...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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: 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...
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
Oralloy
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,...
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
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,...

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.