Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. error in stream reading

error in stream reading

Scheduled Pinned Locked Moved Visual Basic
testingbeta-testinghelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    balakpn
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • B balakpn

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      B 1 Reply Last reply
      0
      • C Christian Graus

        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 )

        B Offline
        B Offline
        balakpn
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • B balakpn

          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

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          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 )

          L 1 Reply Last reply
          0
          • C Christian Graus

            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 )

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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]

            B 1 Reply Last reply
            0
            • L Luc Pattyn

              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]

              B Offline
              B Offline
              balakpn
              wrote on last edited by
              #6

              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

              L 1 Reply Last reply
              0
              • B balakpn

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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]

                B 1 Reply Last reply
                0
                • L Luc Pattyn

                  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]

                  B Offline
                  B Offline
                  balakpn
                  wrote on last edited by
                  #8

                  hi its working fine i made a minor mistake thts the problem sorry and thanks a lot

                  with regards Balagurunathan.B

                  L 1 Reply Last reply
                  0
                  • B balakpn

                    hi its working fine i made a minor mistake thts the problem sorry and thanks a lot

                    with regards Balagurunathan.B

                    L Offline
                    L Offline
                    Luc Pattyn
                    wrote on last edited by
                    #9

                    you're welcome.

                    Luc Pattyn [My Articles] [Forum Guidelines]

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups