By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,540 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

Writing a file and reading same file simultaneously

P: n/a
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/..
Aug 1 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Try using a FileShare mode while opening a file. It allows shared
access to the file. More about it at:

http://msdn2.microsoft.com/en-us/library/y973b725.aspx

Thanks,
Avanti Ketkar

Raghu wrote:
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/..
Aug 1 '06 #2

P: n/a
I tried that but it did not like it.

"avanti" <av****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
Try using a FileShare mode while opening a file. It allows shared
access to the file. More about it at:

http://msdn2.microsoft.com/en-us/library/y973b725.aspx

Thanks,
Avanti Ketkar

Raghu wrote:
>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/..

Aug 1 '06 #3

P: n/a
Oh. What is the error you are getting? Can you give the code snippet?

Thanks,
Avanti

Raghu wrote:
I tried that but it did not like it.

"avanti" <av****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
Try using a FileShare mode while opening a file. It allows shared
access to the file. More about it at:

http://msdn2.microsoft.com/en-us/library/y973b725.aspx

Thanks,
Avanti Ketkar

Raghu wrote:
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/..
Aug 2 '06 #4

P: n/a
It is the same error that you get when you don't use FileShare ...

"avanti" <av****@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Oh. What is the error you are getting? Can you give the code snippet?

Thanks,
Avanti

Raghu wrote:
>I tried that but it did not like it.

"avanti" <av****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegr oups.com...
Try using a FileShare mode while opening a file. It allows shared
access to the file. More about it at:

http://msdn2.microsoft.com/en-us/library/y973b725.aspx

Thanks,
Avanti Ketkar

Raghu wrote:
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/..

Aug 3 '06 #5

P: n/a
If you have a working model on this, I would appreciate if you can post it.

Thanks.
"avanti" <av****@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
Oh. What is the error you are getting? Can you give the code snippet?

Thanks,
Avanti

Raghu wrote:
>I tried that but it did not like it.

"avanti" <av****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegr oups.com...
Try using a FileShare mode while opening a file. It allows shared
access to the file. More about it at:

http://msdn2.microsoft.com/en-us/library/y973b725.aspx

Thanks,
Avanti Ketkar

Raghu wrote:
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/..

Aug 3 '06 #6

P: n/a
<"Raghu" <RaghuNoSpam>wrote:
If you have a working model on this, I would appreciate if you can post it.
Why don't you give us a short but complete program which shows the
problem instead?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 3 '06 #7

P: n/a
Sure! The MSDN link had this code. They have an example of how the file
is not shared. I changed it a bit to demonstrate how it CAN be shared.

using System;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{
string path = @"c:\result.txt";

// Create the file if it exists.
if (!File.Exists(path))
{
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is
some text in the file.");

// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}

// Open the stream and read it back.
using (FileStream fs = File.Open(path, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.Length) 0)
{
Console.WriteLine(temp.GetString(b));
}

try
{
// Try to get another handle to the same file.
using (FileStream fs2 = File.Open(path, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{
// Do some task here.
}
}
catch (Exception e)
{
Console.Write("Opening the file twice is disallowed.");
Console.WriteLine(", as expected: {0}", e.ToString());
}
}
}
}
Thanks,
Avanti Ketkar
Jon wrote:
<"Raghu" <RaghuNoSpam>wrote:
If you have a working model on this, I would appreciate if you can post it.

Why don't you give us a short but complete program which shows the
problem instead?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 4 '06 #8

P: n/a
I don't think this is what the OP is looking at.
While it's true that you have two streams sharing the same file, you aren't
actually reading and writing simultaneously. To do this you need two
separate threads, one for the reads, another for the writes and a bunch of
locking/synchronizing code in order to coordinate the accesses.

Willy.

"avanti" <av****@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
| Sure! The MSDN link had this code. They have an example of how the file
| is not shared. I changed it a bit to demonstrate how it CAN be shared.
|
| using System;
| using System.IO;
| using System.Text;
|
| class Test
| {
| public static void Main()
| {
| string path = @"c:\result.txt";
|
| // Create the file if it exists.
| if (!File.Exists(path))
| {
| // Create the file.
| using (FileStream fs = File.Create(path))
| {
| Byte[] info = new UTF8Encoding(true).GetBytes("This is
| some text in the file.");
|
| // Add some information to the file.
| fs.Write(info, 0, info.Length);
| }
| }
|
| // Open the stream and read it back.
| using (FileStream fs = File.Open(path, FileMode.Open,
| FileAccess.Read, FileShare.ReadWrite))
| {
| byte[] b = new byte[1024];
| UTF8Encoding temp = new UTF8Encoding(true);
|
| while (fs.Read(b, 0, b.Length) 0)
| {
| Console.WriteLine(temp.GetString(b));
| }
|
| try
| {
| // Try to get another handle to the same file.
| using (FileStream fs2 = File.Open(path, FileMode.Open,
| FileAccess.Read, FileShare.ReadWrite))
| {
| // Do some task here.
| }
| }
| catch (Exception e)
| {
| Console.Write("Opening the file twice is disallowed.");
| Console.WriteLine(", as expected: {0}", e.ToString());
| }
| }
| }
| }
|
|
| Thanks,
| Avanti Ketkar
| Jon wrote:
| <"Raghu" <RaghuNoSpam>wrote:
| If you have a working model on this, I would appreciate if you can
post it.
| >
| Why don't you give us a short but complete program which shows the
| problem instead?
| >
| See http://www.pobox.com/~skeet/csharp/complete.html for details of
| what I mean by that.
| >
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
|
Aug 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.