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. read line by line

read line by line

Scheduled Pinned Locked Moved Visual Basic
10 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.
  • L Offline
    L Offline
    ltt19
    wrote on last edited by
    #1

    Hi, I have a string variable and it has over 300000 chars.... it is like it: text text1 txt2 it has many lines, and so, I have to read one by one. I tryed to use System.IO.StreamReader, but it does not accept over 260 chars... If someone has any ideas it would be very helpfulll Thanks in advance

    M 1 Reply Last reply
    0
    • L ltt19

      Hi, I have a string variable and it has over 300000 chars.... it is like it: text text1 txt2 it has many lines, and so, I have to read one by one. I tryed to use System.IO.StreamReader, but it does not accept over 260 chars... If someone has any ideas it would be very helpfulll Thanks in advance

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Maybe this isn't the best way to handle it, but how about using .Split() on the string, supplying the newline character?

      Dim lines() as String = _
      myBigString.Split(System.Environment.NewLine)

      Dim i as integer
      For i = 0 to lines.Length - 1
      '--do something with the line
      DoSomething(lines(i))
      Next

      L 1 Reply Last reply
      0
      • M Mike Ellison

        Maybe this isn't the best way to handle it, but how about using .Split() on the string, supplying the newline character?

        Dim lines() as String = _
        myBigString.Split(System.Environment.NewLine)

        Dim i as integer
        For i = 0 to lines.Length - 1
        '--do something with the line
        DoSomething(lines(i))
        Next

        L Offline
        L Offline
        ltt19
        wrote on last edited by
        #3

        Hi, Yes it should works, but the problem is that I will make a copy of my big string, and it will use more memory. I am thinking about use your method, but them clear my big string... I will try to get a better way... Thanks, it could be usefull

        M D 2 Replies Last reply
        0
        • L ltt19

          Hi, Yes it should works, but the problem is that I will make a copy of my big string, and it will use more memory. I am thinking about use your method, but them clear my big string... I will try to get a better way... Thanks, it could be usefull

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          How about using a StringReader then?

          Dim sr as System.IO.StringReader = _
          new System.IO.StringReader(myBigString)

          Dim line as String
          '--read the first line from the string
          line = sr.ReadLine()
          Do While Not (line Is Nothing)
          '--do something with the line
          DoSomething(line)
          '--read the next line from the string
          line = sr.ReadLine()
          Loop

          L 1 Reply Last reply
          0
          • M Mike Ellison

            How about using a StringReader then?

            Dim sr as System.IO.StringReader = _
            new System.IO.StringReader(myBigString)

            Dim line as String
            '--read the first line from the string
            line = sr.ReadLine()
            Do While Not (line Is Nothing)
            '--do something with the line
            DoSomething(line)
            '--read the next line from the string
            line = sr.ReadLine()
            Loop

            L Offline
            L Offline
            ltt19
            wrote on last edited by
            #5

            Yes... I tryed just with the StreamReader, and it does not work, I have not thinked about StringReader. So I will se it... Thanks

            1 Reply Last reply
            0
            • L ltt19

              Hi, Yes it should works, but the problem is that I will make a copy of my big string, and it will use more memory. I am thinking about use your method, but them clear my big string... I will try to get a better way... Thanks, it could be usefull

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Hi, Since String are immutable, any changes you make to the String, like removing a line from it, will result in a copy of the string being created that includes the change you made, then the original string is abandoned and, later, cleaned up by the garbage collector. Holding a 300K string in memory doesn't sound like a good use of resources. Why the large string? RageInTheMachine9532

              L 1 Reply Last reply
              0
              • D Dave Kreskowiak

                Hi, Since String are immutable, any changes you make to the String, like removing a line from it, will result in a copy of the string being created that includes the change you made, then the original string is abandoned and, later, cleaned up by the garbage collector. Holding a 300K string in memory doesn't sound like a good use of resources. Why the large string? RageInTheMachine9532

                L Offline
                L Offline
                ltt19
                wrote on last edited by
                #7

                Hi, I have a .txt file, and it is very big, my application needs to change this file at any time, and to open edit and save it will result in a long time token, so I think it is better to copy it to a variable, make the changes to the variable, and in the end of the program, it could write again. I do not know if there is a way to rewrite the file just where it has changed, and do not write all the file again, them it will be much better. If you could help me... Thanks

                D 1 Reply Last reply
                0
                • L ltt19

                  Hi, I have a .txt file, and it is very big, my application needs to change this file at any time, and to open edit and save it will result in a long time token, so I think it is better to copy it to a variable, make the changes to the variable, and in the end of the program, it could write again. I do not know if there is a way to rewrite the file just where it has changed, and do not write all the file again, them it will be much better. If you could help me... Thanks

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  What's in this file? Is it a text document? flat database? Worlds Largest Configuration (.INI) file? Maybe there is a better way to do it, but it depends on the what the data is and what your doing to manipulate it. RageInTheMachine9532

                  L 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    What's in this file? Is it a text document? flat database? Worlds Largest Configuration (.INI) file? Maybe there is a better way to do it, but it depends on the what the data is and what your doing to manipulate it. RageInTheMachine9532

                    L Offline
                    L Offline
                    ltt19
                    wrote on last edited by
                    #9

                    Hi, It is a text document, so i read it, edit it and them it write again, but jsut write wher it has changed, the it wil be faster... I do not know with there is a direct way to edit it, or if I have to put it in a variable anyway... Any ideas...

                    D 1 Reply Last reply
                    0
                    • L ltt19

                      Hi, It is a text document, so i read it, edit it and them it write again, but jsut write wher it has changed, the it wil be faster... I do not know with there is a direct way to edit it, or if I have to put it in a variable anyway... Any ideas...

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      OK. It looks like that a String() is the best method then. It sounds like the problem you have now is the speed at which the string is read from or written to the file. I've come up with a partial implementation of a text editor with some code that is the fastest text file reader/writer that I can come up with. It reads/writes 500K in less than 0.05 seconds on my machine. Here's the code snippet for reading and writing the file:

                      Private Sub mnuFileOpen\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
                          If ofdOpenFile.ShowDialog = DialogResult.OK Then
                              fileName = ofdOpenFile.FileName
                              Dim openFile As New StreamReader(fileName)
                              txtEdit.Text = openFile.ReadToEnd()
                              openFile.Close()
                          End If
                      End Sub
                      
                      Private Sub mnuFileSave\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click
                          If Not fileName.Equals(String.Empty) Then
                              Dim saveFileWriter As StreamWriter = File.CreateText(fileName)
                              Dim strReader As New StringReader(txtEdit.Text)
                              saveFileWriter.Write(strReader.ReadToEnd)
                              saveFileWriter.Flush()
                              saveFileWriter.Close()
                              strReader.Close()
                          End If
                      End Sub
                      

                      If you want the entire sample project, send me an email with an address to reply to. RageInTheMachine9532

                      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