473,775 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(strT odaysLogFile, System.IO.FileM ode.Append,
System.IO.FileA ccess.Write, System.IO.FileS hare.Read);
this.m_swLog = new StreamWriter(th is.m_fsLog);
if(this.m_swLog != null) this.m_swLog.Au toFlush = true;
}

pubic WriteLogMessage ()
{
if(this.m_swLog != null) this.m_swLog.Wr ite("[" +
DateTime.Now.To String("HH:mm:s s") + "] " + aMsg + Environment.New Line);
}
</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.FileS tream fs = new FileStream(this .m_strCurrentFi leName,
System.IO.FileM ode.Open, System.IO.FileA ccess.Read, System.IO.FileS hare.Read);
System.IO.Strea mReader sr = new StreamReader(fs );

while(sr.Peek() > -1) this.DisplayLog Text(sr.ReadLin e() +
Environment.New Line);

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 6768
cnu wrote:
this.m_fsLog = new FileStream(strT odaysLogFile, System.IO.FileM ode.Append,
System.IO.FileA ccess.Write, System.IO.FileS hare.Read);
This part is OK. You are opening the file for writing
(FileAccess.Wri te) and you grant other processes the right to Read
(FileShare.Read ).
System.IO.FileS tream fs = new FileStream(this .m_strCurrentFi leName,
System.IO.FileM ode.Open, System.IO.FileA ccess.Read, System.IO.FileS hare.Read);
This is where the problem is. You open the file for reading
(FileAccess.Rea d) 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.ReadW rite 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
1495
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 the process has completed executing. My problem is I need to get the output in the label simultaneously when the EXE is running, not after it has completed executing. Just like when I run the Exe in the cmd prompt. In my code I am opening two...
1
303
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 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);
8
23907
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
2549
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 this? I am assuming my card is full duplex, it is a built-in sound card on a new dell 600m laptop, but I am not sure how to tell for sure. But I think the problem is not so much my sound card, but that I am making some fundamentally wrong...
1
1851
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 allow everyone to read/write/delete the .mdl files created by the cygwin cron jobs, everything works. If I don't, IIS/ASP.NET cannot open the MSAccess database. How do I write a little web page to see what account I am running under? I think I...
8
10538
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? Thanks. Raghu/..
12
5470
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 author of one of the python database clients mentioned that using one thread to retrieve the data from the oracle database and another to insert the data into postgresql with something like a pipe between the two threads might make sense, keeping...
0
789
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14056
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
0
9622
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...
0
9454
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,...
0
10270
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, 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...
0
10109
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 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...
1
10051
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
9916
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
8939
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
7464
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...
3
2853
bsmnconsultancy
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...

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.