error in stream reading
-
hi i am doing a project in which creating log files for the users signed on . its automated input process so i will be writing each and every input in log file . log file is a .txt file i am appening the file using streamwriter at the same time i am trying with another button to read the file using stram reader or any io.files.readalllines() but its not allowing to read. how i can read the file while some strea writes into it. please any one tell me this. i cannot close the stream each and every time as the process is automated for testing purpose.
with regards Balagurunathan.B
-
hi i am doing a project in which creating log files for the users signed on . its automated input process so i will be writing each and every input in log file . log file is a .txt file i am appening the file using streamwriter at the same time i am trying with another button to read the file using stram reader or any io.files.readalllines() but its not allowing to read. how i can read the file while some strea writes into it. please any one tell me this. i cannot close the stream each and every time as the process is automated for testing purpose.
with regards Balagurunathan.B
What you want to do is impossible. You absolutely need to close the stream, in order to read it.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
What you want to do is impossible. You absolutely need to close the stream, in order to read it.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
but my doubt is if you r opening and editing a word document and on the same time if some other user on network trys to read they can open in read only mode and can read lastly saved one then why can us do the same programatically thanks
with regards Balagurunathan.B
-
but my doubt is if you r opening and editing a word document and on the same time if some other user on network trys to read they can open in read only mode and can read lastly saved one then why can us do the same programatically thanks
with regards Balagurunathan.B
Word handles this by creating a temporary file, which is the file that is opened for editing. Read only mode means it reads the entire file, then closes it, it doesn't keep it open. Open a Word doc, then look in Windows Explorer, you'll see the temporary file there.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Word handles this by creating a temporary file, which is the file that is opened for editing. Read only mode means it reads the entire file, then closes it, it doesn't keep it open. Open a Word doc, then look in Windows Explorer, you'll see the temporary file there.
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
Hi, I beg to differ: whoever creates/opens a file is in control regarding the operations others can do to the same file. one can easily share a file between threads and even processes; it suffices to apply the correct FileShare value when creating/opening the file. My example shows a first stream writing to a file it allows others to read, and a second stream reading from same file allowing others everything:
string filename="streamTest.txt";
using(FileStream fw=new FileStream(filename, FileMode.Create,
FileAccess.Write, FileShare.Read)) {
using(StreamWriter tw=new StreamWriter(fw)) {
tw.WriteLine("Created new file");
using(FileStream fr=new FileStream(filename, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)) {
using(StreamReader tr=new StreamReader(fr)) {
for(int i=0; i<10; i++) {
tw.WriteLine("line "+i);
tw.Flush();
string s=tr.ReadLine();
Console.WriteLine(s);
}
}
}
}
}BTW the flush is there to undo the buffering that takes place in these streams; without it there probably would be nothing to read when the short loop terminates; for longer streams, flushing is not needed ! And normally the reader should continue to read after the writer has done, my example does not. :) PS: sorry for posting a C# example in a VB.NET forum !
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, I beg to differ: whoever creates/opens a file is in control regarding the operations others can do to the same file. one can easily share a file between threads and even processes; it suffices to apply the correct FileShare value when creating/opening the file. My example shows a first stream writing to a file it allows others to read, and a second stream reading from same file allowing others everything:
string filename="streamTest.txt";
using(FileStream fw=new FileStream(filename, FileMode.Create,
FileAccess.Write, FileShare.Read)) {
using(StreamWriter tw=new StreamWriter(fw)) {
tw.WriteLine("Created new file");
using(FileStream fr=new FileStream(filename, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite)) {
using(StreamReader tr=new StreamReader(fr)) {
for(int i=0; i<10; i++) {
tw.WriteLine("line "+i);
tw.Flush();
string s=tr.ReadLine();
Console.WriteLine(s);
}
}
}
}
}BTW the flush is there to undo the buffering that takes place in these streams; without it there probably would be nothing to read when the short loop terminates; for longer streams, flushing is not needed ! And normally the reader should continue to read after the writer has done, my example does not. :) PS: sorry for posting a C# example in a VB.NET forum !
Luc Pattyn [My Articles] [Forum Guidelines]
-
hi Luc Pattyn :doh: but its not working mate i am appending the string is it because of that something else
with regards Balagurunathan.B
balakpn wrote:
not working
please be more specific: if it throws an exception show it; if it runs but does not produce the expected outcome, please explain. I did run my example before posting it. Much depends on the FileMode, FileAccess and FileShare values you use EVERY time the file or filestream gets created/opened. e.g. the simplest overloads for creatinbg/opening a file (those with default values for FileShare) would use FileShare.None, disallowing others to touch the file. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
balakpn wrote:
not working
please be more specific: if it throws an exception show it; if it runs but does not produce the expected outcome, please explain. I did run my example before posting it. Much depends on the FileMode, FileAccess and FileShare values you use EVERY time the file or filestream gets created/opened. e.g. the simplest overloads for creatinbg/opening a file (those with default values for FileShare) would use FileShare.None, disallowing others to touch the file. :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
hi its working fine i made a minor mistake thts the problem sorry and thanks a lot
with regards Balagurunathan.B
you're welcome.
Luc Pattyn [My Articles] [Forum Guidelines]