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. C#
  4. Read large text files with c#

Read large text files with c#

Scheduled Pinned Locked Moved C#
csharpperformancehelptutorial
12 Posts 5 Posters 1 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.
  • M Offline
    M Offline
    MrWeiland
    wrote on last edited by
    #1

    I know that i cannot read lets say 80mb big file into a textbox, first i takes too long and second it would take alot of memory. I need to find a approach to read sections from file to textbox and when user scrolls down or push page down it read another section... Is that a good approach to problem or do anyone have another approach to read large textfiles fast... <Im really impressed what Christan Ghisler done with his lister, its really fast.>) Im open for ideas how to solve this problem... -- modified at 12:13 Tuesday 7th March, 2006

    W G J 3 Replies Last reply
    0
    • M MrWeiland

      I know that i cannot read lets say 80mb big file into a textbox, first i takes too long and second it would take alot of memory. I need to find a approach to read sections from file to textbox and when user scrolls down or push page down it read another section... Is that a good approach to problem or do anyone have another approach to read large textfiles fast... <Im really impressed what Christan Ghisler done with his lister, its really fast.>) Im open for ideas how to solve this problem... -- modified at 12:13 Tuesday 7th March, 2006

      W Offline
      W Offline
      Werdna
      wrote on last edited by
      #2

      I have created a simple program that does something similar. It will read a file in chunks using a reader and provides forward only navigation. If you send me an email I can send you the source code (C# vs2005) andrew at pmall dot com

      M 1 Reply Last reply
      0
      • M MrWeiland

        I know that i cannot read lets say 80mb big file into a textbox, first i takes too long and second it would take alot of memory. I need to find a approach to read sections from file to textbox and when user scrolls down or push page down it read another section... Is that a good approach to problem or do anyone have another approach to read large textfiles fast... <Im really impressed what Christan Ghisler done with his lister, its really fast.>) Im open for ideas how to solve this problem... -- modified at 12:13 Tuesday 7th March, 2006

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        You basically have two options: 1. Open the file as a textfile, but then you can only read it sequentially. 2. Open the file as a binary file, then you have to detect the line breaks yourself, and convert the bytes to text using the Encoding class, but you can read from any part of the file. --- b { font-weight: normal; }

        M 1 Reply Last reply
        0
        • M MrWeiland

          I know that i cannot read lets say 80mb big file into a textbox, first i takes too long and second it would take alot of memory. I need to find a approach to read sections from file to textbox and when user scrolls down or push page down it read another section... Is that a good approach to problem or do anyone have another approach to read large textfiles fast... <Im really impressed what Christan Ghisler done with his lister, its really fast.>) Im open for ideas how to solve this problem... -- modified at 12:13 Tuesday 7th March, 2006

          J Offline
          J Offline
          JoeSox
          wrote on last edited by
          #4

          System.IO.File in .Net 2.0 is really fast and has some new methods like ReadAllText() and ReadAllLines(). Why not some custom methods for your app? Later, JoeSox "Football is a game of cliches, and I believe in every one of them." -Vincent Lombardi CPMCv1.0 ↔ humanaiproject.org ↔ Last.fm

          M H 2 Replies Last reply
          0
          • W Werdna

            I have created a simple program that does something similar. It will read a file in chunks using a reader and provides forward only navigation. If you send me an email I can send you the source code (C# vs2005) andrew at pmall dot com

            M Offline
            M Offline
            MrWeiland
            wrote on last edited by
            #5

            Sent you a mail :)

            1 Reply Last reply
            0
            • J JoeSox

              System.IO.File in .Net 2.0 is really fast and has some new methods like ReadAllText() and ReadAllLines(). Why not some custom methods for your app? Later, JoeSox "Football is a game of cliches, and I believe in every one of them." -Vincent Lombardi CPMCv1.0 ↔ humanaiproject.org ↔ Last.fm

              M Offline
              M Offline
              MrWeiland
              wrote on last edited by
              #6

              How do you mean you should do then?

              J 1 Reply Last reply
              0
              • G Guffa

                You basically have two options: 1. Open the file as a textfile, but then you can only read it sequentially. 2. Open the file as a binary file, then you have to detect the line breaks yourself, and convert the bytes to text using the Encoding class, but you can read from any part of the file. --- b { font-weight: normal; }

                M Offline
                M Offline
                MrWeiland
                wrote on last edited by
                #7

                I dont quite get what you are saying here.. In this case it would be possible to read chunks from whereever you like in file...or did i dont understand what you saying? FileStream fs=new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 8192 ); //we use the default buffer StringBuilder result=new StringBuilder(8192); byte[] buffer=new byte[8192]; int count=fs.Read(buffer,0,8192); //reading a block from the file, here we could start for example on 2000 instead of pos 0 //(fs.Read(buffer,2000,8192)) ASCIIEncoding encoding=new ASCIIEncoding(); while (count!=0) //stop when Read returns 0 { result.Append(encoding.GetString(buffer)); count=fs.Read(buffer,0,8192); } fs.Seek(0, SeekOrigin.Begin);

                G 1 Reply Last reply
                0
                • M MrWeiland

                  I dont quite get what you are saying here.. In this case it would be possible to read chunks from whereever you like in file...or did i dont understand what you saying? FileStream fs=new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 8192 ); //we use the default buffer StringBuilder result=new StringBuilder(8192); byte[] buffer=new byte[8192]; int count=fs.Read(buffer,0,8192); //reading a block from the file, here we could start for example on 2000 instead of pos 0 //(fs.Read(buffer,2000,8192)) ASCIIEncoding encoding=new ASCIIEncoding(); while (count!=0) //stop when Read returns 0 { result.Append(encoding.GetString(buffer)); count=fs.Read(buffer,0,8192); } fs.Seek(0, SeekOrigin.Begin);

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #8

                  Yes, you can use the Seek method to go to any position in the file. Note: There is an error in this comment in the code:

                  //reading a block from the file, here we could start for example on 2000 instead of pos 0 //(fs.Read(buffer,2000,8192))

                  Using that call would result in an error message. As the size of the array is 8192 bytes, you can't read 8192 bytes and put them in the array from index 2000. Between index 2000 and the end of the array there is only room for 6192 bytes. --- b { font-weight: normal; }

                  M 1 Reply Last reply
                  0
                  • J JoeSox

                    System.IO.File in .Net 2.0 is really fast and has some new methods like ReadAllText() and ReadAllLines(). Why not some custom methods for your app? Later, JoeSox "Football is a game of cliches, and I believe in every one of them." -Vincent Lombardi CPMCv1.0 ↔ humanaiproject.org ↔ Last.fm

                    H Offline
                    H Offline
                    hackerhcm
                    wrote on last edited by
                    #9

                    ReadAllText() is synchronous method, when you try to read a large text file, your application will hang or slow down ... better to use asynchronous medthod !!!

                    J 1 Reply Last reply
                    0
                    • G Guffa

                      Yes, you can use the Seek method to go to any position in the file. Note: There is an error in this comment in the code:

                      //reading a block from the file, here we could start for example on 2000 instead of pos 0 //(fs.Read(buffer,2000,8192))

                      Using that call would result in an error message. As the size of the array is 8192 bytes, you can't read 8192 bytes and put them in the array from index 2000. Between index 2000 and the end of the array there is only room for 6192 bytes. --- b { font-weight: normal; }

                      M Offline
                      M Offline
                      MrWeiland
                      wrote on last edited by
                      #10

                      You are so right, little mistake from my side :-O

                      1 Reply Last reply
                      0
                      • H hackerhcm

                        ReadAllText() is synchronous method, when you try to read a large text file, your application will hang or slow down ... better to use asynchronous medthod !!!

                        J Offline
                        J Offline
                        JoeSox
                        wrote on last edited by
                        #11

                        hackerhcm wrote:

                        ReadAllText() is synchronous method, when you try to read a large text file, your application will hang or slow down ... better to use asynchronous medthod !!!

                        Of course, I was thinking of using ReadAllText() within the asynchronous medthod. Just read chunks of the 80mb from a temp file or something using ReadAllText(), when the user nears the end point create then read another chunk. Later, JoeSox "Football is a game of cliches, and I believe in every one of them." -Vincent Lombardi CPMCv1.0 ↔ humanaiproject.org ↔ Last.fm

                        1 Reply Last reply
                        0
                        • M MrWeiland

                          How do you mean you should do then?

                          J Offline
                          J Offline
                          JoeSox
                          wrote on last edited by
                          #12

                          I was thinking of using ReadAllText() within an asynchronous method. Just read chunks of the 80mb from a temp file using ReadAllText(), when the user nears the end point create, then read another chunk into the textbox.AppendText(tempfile.ReadAllText()). Later, JoeSox "Football is a game of cliches, and I believe in every one of them." -Vincent Lombardi CPMCv1.0 ↔ humanaiproject.org ↔ Last.fm

                          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