Home > Net >  Use StreamReader () constructor open text files, abnormal, prompt occupied by another process. Why i
Use StreamReader () constructor open text files, abnormal, prompt occupied by another process. Why i

Time:09-22



There is a TXT file, is a c + + written procedures so open, update content:

The FILE * fp=fopen (" a.t xt ", "w");


The other python programs, would open the fast reading a line, briefly, the code simpler, as follows:

Fp=open (" a.t xt ", "r")

Fp. Readline ();

Fp. Close ();


Now I use c # write a console application,

Using (StreamReader sr=new StreamReader (" a.t xt "))
{
The sr. ReadLine ();
}


Now the problem is, the c # program, through using the line, the tectonic streamreader error, tip: open the a.t xt there is abnormal, because by other processes is open!

The error information screenshot as shown in figure,



It.however, just open read-only in c # and c + + program and open the file in python, there is no specified is exclusive to open, so you should open no problem ah,
And, in fact, a python program similar to open and read, all operating normally,

Why c # code, just open it?


See # mentioned in the document, the FileStream class a FileShare attribute, you can specify whether the monopoly to open,
The c # code here, however, using StreamReader class, this property;
And with the Python code in c + + code, only simple fopen/open, should also is exclusive,


So, how to view, who is in the open the TXT? Why can't play in c # code to read?

Master students, please grant instruction comment, thank you, wish you a happy Spring Festival!

CodePudding user response:

Look at the WhoLockMe this software

CodePudding user response:

Shared control for the sake of safety, is the purpose of sharing hypothesis has the following situation:
A, B
-- -- -- -- -- -- -- -- -- -- --
Read a read
The second reading written
Three write write

It is obvious that a situation is safe, many readers will not affect each other,
Obviously, it is not safe to three, more will be written mutual stepping on my toes;

But two?
The answer is case 2 is unsafe because of B write (write) can be anywhere in the operation, not A security of read operation, according to his logic

If A knowing that not safe, knowing that might dirty read, to read operation, then under the DotNet,
1, B to allow others to Read, must be at least FileShare. Sharing the Read
2, a. to recognize unsafe operation, agreed to FileShare. ReadWrite ,
 var A=new StreamWriter (filename);//Shared by default FileShare. Read [1], allowing others to dirty reads, 
Var B=new StreamReader (filename);//Shared by default FileShare. Read [2], not dirty reads, failure,

 var A=new StreamWriter (filename); 
Var B=new StreamReader (new FileStream (filename, FileMode. Open, FileAccess. Read, FileShare. ReadWrite)); Dirty read//recognition, successful,



As to why a python program can read, do you want to see the documentation and the specific implementation,
Such as fopen may call _fsopen, _fsopen shflag specified Shared schema,


[note:]
StreamWriter source code (line 226) current
https://referencesource.microsoft.com/#mscorlib/system/io/streamwriter.cs

[note:]
StreamReader source code (line 240) current
https://referencesource.microsoft.com/#mscorlib/system/io/streamreader.cs

CodePudding user response:

File Read default is exclusive, you need to set up open for sharing,

CodePudding user response:

Streamreader (FileStream) create classes, parameters using the File, Open (string, mode, Access and share) to create the FileStream, with the Open mode, Access to use the read, share, try the readwrite?

CodePudding user response:

refer to the second floor github_36000833 response:
...
If A knowing that not safe, knowing that might dirty read, to read operation, then under the DotNet,
1, B to allow others to Read, must be at least FileShare. Sharing the Read
2, a. to recognize unsafe operation, agreed to FileShare. ReadWrite ,
.


Windows can refer to the official documents to read and write in sharing compatible form:
https://docs.microsoft.com/en-us/windows/win32/fileio/creating-and-opening-files

 
The first to open the file way legal way to open the second
GENERIC_WRITE, FILE_SHARE_READ GENERIC_READ, FILE_SHARE_WRITE
GENERIC_WRITE, FILE_SHARE_READ GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE
.

CodePudding user response:

That you normally use to allow the dirty read or do not allow the dirty read write
  •  Tags:  
  • C#