stream reader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aleks Kleyn

    #1

    stream reader

    I use streamReader to read files from hard drive. Some files which I read
    have spaces at the end of line. However whatever code I use
    StreamReader1 = System.IO.File. OpenText(file)
    or
    StreamReader1 = New System.IO.Strea mReader(file)
    and then
    StreamReader1.R eadToEnd()
    or
    StreamReader1.R eadLine()
    I am losing spaces in the response. What I need to change to read all data.
    Aleks Kleyn

  • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

    #2
    Re: stream reader

    Aleks Kleyn wrote:
    I use streamReader to read files from hard drive. Some files which I
    read have spaces at the end of line. However whatever code I use
    StreamReader1 = System.IO.File. OpenText(file)
    or
    StreamReader1 = New System.IO.Strea mReader(file)
    and then
    StreamReader1.R eadToEnd()
    or
    StreamReader1.R eadLine()
    I am losing spaces in the response. What I need to change to read all data.
    Aleks Kleyn
    Are you sure that you are losing the spaces when reading the text from
    the file? Have you verified that the file is actually containing those
    spaces, and that they really are space characters?

    It makes no sense that anything would be removed when reading a file.
    What do you do with the text after reading it, and how do you determine
    that there are spaces missing?

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: stream reader

      "Aleks Kleyn" <Aleks_Kleyn@Ma ilAps.orgschrie b:
      >I use streamReader to read files from hard drive. Some files which I read
      >have spaces at the end of line. However whatever code I use
      StreamReader1 = System.IO.File. OpenText(file)
      or
      StreamReader1 = New System.IO.Strea mReader(file)
      and then
      StreamReader1.R eadToEnd()
      or
      StreamReader1.R eadLine()
      I am losing spaces in the response. What I need to change to read all
      data.

      How do you take a look at the data read from the file?

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Aleks Kleyn

        #4
        Re: stream reader

        It is very simple. I know place where line of file has spaces in the end
        (for instance "ba ". Then I put break point at place where I read the
        file, copy the value of variable into notepad and see that after charecter
        'a' next line follows imediately. Discovered I this because stream was
        procede not way as I expected.

        Aleks Kleyn

        Comment

        • Michael C

          #5
          Re: stream reader

          "Aleks Kleyn" <Aleks_Kleyn@Ma ilAps.orgwrote in message
          news:LSmLh.44$B x.25@newsfe12.l ga...
          It is very simple. I know place where line of file has spaces in the end
          (for instance "ba ". Then I put break point at place where I read the
          file, copy the value of variable into notepad and see that after charecter
          'a' next line follows imediately. Discovered I this because stream was
          procede not way as I expected.
          I would expect this is a feature of the streamreader. Try opening the file
          using a filestream instead.
          >
          Aleks Kleyn

          Comment

          • Chris Dunaway

            #6
            Re: stream reader

            On Mar 19, 12:42 am, "Michael C" <nos...@nospam. comwrote:
            "Aleks Kleyn" <Aleks_Kl...@Ma ilAps.orgwrote in message
            >
            news:LSmLh.44$B x.25@newsfe12.l ga...
            >
            It is very simple. I know place where line of file has spaces in the end
            (for instance "ba ". Then I put break point at place where I read the
            file, copy the value of variable into notepad and see that after charecter
            'a' next line follows imediately. Discovered I this because stream was
            procede not way as I expected.
            >
            I would expect this is a feature of the streamreader. Try opening the file
            using a filestream instead.
            >
            >
            >
            Aleks Kleyn
            No, something else must be happening here. I use StreamReader all the
            time to read text files and spaces at the ends of the line are always
            read.

            Are the spaces at the end of the line followed by both a carriage
            return and linefeed character?

            Can you show us a simple program that duplicates the error.

            Chris

            Comment

            • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

              #7
              Re: stream reader

              Aleks Kleyn wrote:
              It is very simple. I know place where line of file has spaces in the end
              (for instance "ba ". Then I put break point at place where I read the
              file, copy the value of variable into notepad and see that after
              charecter 'a' next line follows imediately. Discovered I this because
              stream was procede not way as I expected.
              >
              Aleks Kleyn
              Either the spaces aren't there from the start, or you have to do
              something that removes them.

              I tried this code:

              Dim r As StreamReader
              r = File.OpenText(" d:\temp\test.tx t")
              s = r.ReadToEnd
              r.Dispose()

              With the file test.txt containing four lines:
              =============== =====
              asdf
              b
              asdfasdf
              qwerty
              =============== =====

              I put a breakpoint after the code and looked at the contents of the s
              variable, and the spaces are there.

              --
              Göran Andersson
              _____
              Göran Anderssons privata hemsida.

              Comment

              • Aleks Kleyn

                #8
                Re: stream reader

                I think it has carriage
                return and linefeed character, because i edited it by ms studio, and it
                warns if something is absent.
                ..
                The code is
                Dim TextStream As System.IO.Strea mReader
                If (My.Computer.Fi leSystem.FileEx ists(fpath)) Then
                TextStream=new System.IO.Strea mReader(fpath,T rue)
                'TextStream = System.IO.File. OpenText(fpath)
                HtmString = TextStream.Read ToEnd()
                TextStream.Clos e()
                Both cases, comented and uncomented give the same answer.

                Comment

                • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                  #9
                  Re: stream reader

                  Aleks Kleyn wrote:
                  I think it has carriage
                  return and linefeed character, because i edited it by ms studio, and it
                  warns if something is absent.
                  Then it's most likely that program that removes the spaces when you save
                  the file.

                  Open the file in Notepad and check if the spaces are there. It's a plain
                  text editor without any syntax formatting features, so it won't remove
                  characters for you.

                  --
                  Göran Andersson
                  _____
                  Göran Anderssons privata hemsida.

                  Comment

                  • Aleks_Kleyn@MailAPS.org

                    #10
                    Re: stream reader

                    Yesterday after your email I thought that may be problem that I use
                    asp.net code and you visual basic. I repeat this code in visual basic
                    and got the same result (by the way I do not see spaces in your email,
                    at least they disapear in response windows; may be this server has the
                    same problem). Today I tried in VB next code

                    Dim HtmString As String
                    Dim TextStream As System.IO.Strea mReader
                    Dim fso As Object, TextStream1 As Object
                    If (My.Computer.Fi leSystem.FileEx ists(fpath)) Then
                    TextStream = New System.IO.Strea mReader(fpath, True)
                    'TextStream = System.IO.File. OpenText(fpath)
                    HtmString = System.IO.File. ReadAllText(fpa th)
                    HtmString = TextStream.Read ToEnd()
                    TextStream.Clos e()
                    fso = CreateObject("S cripting.FileSy stemObject")
                    TextStream1 = fso.OpenTextFil e(fpath)
                    HtmString1 = TextStream1.Rea dAll()
                    End If

                    HtmString and HtmString1 were different and HtmString1 has all spaces.
                    Aleks Kleyn

                    Comment

                    • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                      #11
                      Re: stream reader

                      Aleks_Kleyn@Mai lAPS.org wrote:
                      Yesterday after your email I thought that may be problem that I use
                      asp.net code and you visual basic.
                      What programming language are you using then? ASP.NET is not a
                      programming language, just a platform. I assumed that you were using
                      VB.NET as you are posting in the VB.NET newsgroup.
                      I repeat this code in visual basic
                      and got the same result (by the way I do not see spaces in your email,
                      at least they disapear in response windows; may be this server has the
                      same problem).
                      You are right, the spaces disappeared from the post. Each line in the
                      file was padded with spaces to eight characters in length.
                      Today I tried in VB next code
                      >
                      Dim HtmString As String
                      Dim TextStream As System.IO.Strea mReader
                      Dim fso As Object, TextStream1 As Object
                      If (My.Computer.Fi leSystem.FileEx ists(fpath)) Then
                      TextStream = New System.IO.Strea mReader(fpath, True)
                      'TextStream = System.IO.File. OpenText(fpath)
                      HtmString = System.IO.File. ReadAllText(fpa th)
                      HtmString = TextStream.Read ToEnd()
                      TextStream.Clos e()
                      fso = CreateObject("S cripting.FileSy stemObject")
                      TextStream1 = fso.OpenTextFil e(fpath)
                      HtmString1 = TextStream1.Rea dAll()
                      End If
                      >
                      HtmString and HtmString1 were different and HtmString1 has all spaces.
                      Aleks Kleyn
                      >
                      The equivalent of the FileSystemObjec t code would be:

                      HtmlString = System.IO.ReadA llText(path, System.Text.Enc oding.ASCII)

                      --
                      Göran Andersson
                      _____
                      Göran Anderssons privata hemsida.

                      Comment

                      • Aleks_Kleyn@MailAPS.org

                        #12
                        Re: stream reader

                        On Mar 20, 2:44 pm, Göran Andersson <g...@guffa.com wrote:
                        Aleks_Kl...@Mai lAPS.org wrote:
                        Yesterday after your email I thought that may be problem that I use
                        asp.net code and you visual basic.
                        >
                        What programming language are you using then? ASP.NET is not a
                        programming language, just a platform. I assumed that you were using
                        VB.NET as you are posting in the VB.NET newsgroup.
                        >
                        I repeat this code in visual basic
                        and got the same result (by the way I do not see spaces in your email,
                        at least they disapear in response windows; may be this server has the
                        same problem).
                        >
                        You are right, the spaces disappeared from the post. Each line in the
                        file was padded with spaces to eight characters in length.
                        >
                        >
                        >
                        >
                        >
                        Today I tried in VB next code
                        >
                        Dim HtmString As String
                        Dim TextStream As System.IO.Strea mReader
                        Dim fso As Object, TextStream1 As Object
                        If (My.Computer.Fi leSystem.FileEx ists(fpath)) Then
                        TextStream = New System.IO.Strea mReader(fpath, True)
                        'TextStream = System.IO.File. OpenText(fpath)
                        HtmString = System.IO.File. ReadAllText(fpa th)
                        HtmString = TextStream.Read ToEnd()
                        TextStream.Clos e()
                        fso = CreateObject("S cripting.FileSy stemObject")
                        TextStream1 = fso.OpenTextFil e(fpath)
                        HtmString1 = TextStream1.Rea dAll()
                        End If
                        >
                        HtmString and HtmString1 were different and HtmString1 has all spaces.
                        Aleks Kleyn
                        >
                        The equivalent of the FileSystemObjec t code would be:
                        >
                        HtmlString = System.IO.ReadA llText(path, System.Text.Enc oding.ASCII)
                        >
                        --
                        Göran Andersson
                        _____http://www.guffa.com- Hide quoted text -
                        >
                        - Show quoted text -
                        i using visual basic.net. There is no difference where I use it: in
                        asp or desktop (the only difference that there is no global vars like
                        we use them in desktop). Today's test showed this one more time.
                        Spaces do not disappear for reason of posting because all code runs on
                        the server.
                        You probably mean System.IO.File. ReadAllText, because IO does not have
                        such method.
                        Adding your encoding turned disappeared spaces to question mark, so
                        they are not really spaces. The question now is to find good encoding,
                        but this is no problem. It is interesting, because file was prepared
                        by ms word.
                        Thank you very much

                        Comment

                        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                          #13
                          Re: stream reader

                          Aleks_Kleyn@Mai lAPS.org wrote:
                          On Mar 20, 2:44 pm, Göran Andersson <g...@guffa.com wrote:
                          >Aleks_Kl...@Ma ilAPS.org wrote:
                          >>Yesterday after your email I thought that may be problem that I use
                          >>asp.net code and you visual basic.
                          >What programming language are you using then? ASP.NET is not a
                          >programming language, just a platform. I assumed that you were using
                          >VB.NET as you are posting in the VB.NET newsgroup.
                          >>
                          >>I repeat this code in visual basic
                          >>and got the same result (by the way I do not see spaces in your email,
                          >>at least they disapear in response windows; may be this server has the
                          >>same problem).
                          >You are right, the spaces disappeared from the post. Each line in the
                          >file was padded with spaces to eight characters in length.
                          >>
                          >>
                          >>
                          >>
                          >>
                          >>Today I tried in VB next code
                          >>Dim HtmString As String
                          >>Dim TextStream As System.IO.Strea mReader
                          >>Dim fso As Object, TextStream1 As Object
                          >>If (My.Computer.Fi leSystem.FileEx ists(fpath)) Then
                          >>TextStream = New System.IO.Strea mReader(fpath, True)
                          >>'TextStream = System.IO.File. OpenText(fpath)
                          >>HtmString = System.IO.File. ReadAllText(fpa th)
                          >>HtmString = TextStream.Read ToEnd()
                          >>TextStream.Cl ose()
                          >>fso = CreateObject("S cripting.FileSy stemObject")
                          >>TextStream1 = fso.OpenTextFil e(fpath)
                          >>HtmString1 = TextStream1.Rea dAll()
                          >>End If
                          >>HtmString and HtmString1 were different and HtmString1 has all spaces.
                          >>Aleks Kleyn
                          >The equivalent of the FileSystemObjec t code would be:
                          >>
                          >HtmlString = System.IO.ReadA llText(path, System.Text.Enc oding.ASCII)
                          >>
                          >--
                          >Göran Andersson
                          >_____http://www.guffa.com- Hide quoted text -
                          >>
                          >- Show quoted text -
                          >
                          i using visual basic.net. There is no difference where I use it: in
                          asp or desktop (the only difference that there is no global vars like
                          we use them in desktop).
                          Actually global variables doesn't exist at all in VB.NET. :) The
                          difference is that you can put variables in the form class in a windows
                          application, and as long as the form is shown the variables remain. In a
                          web application you have a page class instead, and that only lasts the
                          time that it takes to create one page, then it's gone.
                          Today's test showed this one more time.
                          Spaces do not disappear for reason of posting because all code runs on
                          the server.
                          I was talking about the spaces in my file that I was posting in the
                          newsgroup.
                          You probably mean System.IO.File. ReadAllText, because IO does not have
                          such method.
                          Yes, that's right.
                          Adding your encoding turned disappeared spaces to question mark, so
                          they are not really spaces. The question now is to find good encoding,
                          but this is no problem.
                          You can change the file extension to .bin and open it in Visual Studio.
                          That will show you a hex dump representation of the file, where you can
                          see exactly what it contains.

                          --
                          Göran Andersson
                          _____
                          Göran Anderssons privata hemsida.

                          Comment

                          Working...