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. .NET (Core and Framework)
  4. HELP! Reading Files...

HELP! Reading Files...

Scheduled Pinned Locked Moved .NET (Core and Framework)
questionjsonperformancehelptutorial
7 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.
  • M Offline
    M Offline
    mikasa
    wrote on last edited by
    #1

    Ok, in no way am I a N00b at reading files! However, I was debugging some code today in which I was using a IO.StreamReader to read the text from a file and using an IO.StreamWriter to write it into a different file. This file is about 3.2MB. The StreamReader DOES return the Correct Length of the File (IO.StreamReader.BaseStream.Length). HOWEVER, while reading the file, the Stream THINKS it has reached the end, and like a good little programmer, I Flush the StreamWriter (BTW, I am flushing every 8096 Bytes - yes, that IS a lot of water!) but I am missing a HUGE chunk of data at the End of my file! I have noticed that the IO.StreamReader.BaseStream.Position is usually TOTALLY different than the Sum of the Bytes returned for each re-iteration of retrieving data. For example, reading in 8096 Bytes at a time, when my Position variable thinks it's at 16,192, the BaseStream has it's internal buffer set to some other amount... Is this what's messing it up? Notice however, it I REALLY want to waste a lot of memory and processing time, I can just read it all in at once (IO.StreamReader.ReadToEnd) and then process it....GreeeEEEAAAaaaat!!! Just what I want! NOT!!! So...what the @%$* is going on!??? How do I read the rest of the data!??? :wtf:

    M 2 Replies Last reply
    0
    • M mikasa

      Ok, in no way am I a N00b at reading files! However, I was debugging some code today in which I was using a IO.StreamReader to read the text from a file and using an IO.StreamWriter to write it into a different file. This file is about 3.2MB. The StreamReader DOES return the Correct Length of the File (IO.StreamReader.BaseStream.Length). HOWEVER, while reading the file, the Stream THINKS it has reached the end, and like a good little programmer, I Flush the StreamWriter (BTW, I am flushing every 8096 Bytes - yes, that IS a lot of water!) but I am missing a HUGE chunk of data at the End of my file! I have noticed that the IO.StreamReader.BaseStream.Position is usually TOTALLY different than the Sum of the Bytes returned for each re-iteration of retrieving data. For example, reading in 8096 Bytes at a time, when my Position variable thinks it's at 16,192, the BaseStream has it's internal buffer set to some other amount... Is this what's messing it up? Notice however, it I REALLY want to waste a lot of memory and processing time, I can just read it all in at once (IO.StreamReader.ReadToEnd) and then process it....GreeeEEEAAAaaaat!!! Just what I want! NOT!!! So...what the @%$* is going on!??? How do I read the rest of the data!??? :wtf:

      M Offline
      M Offline
      mikasa
      wrote on last edited by
      #2

      Wait a tick, it gets even better! I just discovered that it's Reading past the end of the File and returning more data than it should! So, I know I need to use this handy-dandy Routine MS gave us: IO.StreamReader.DiscardBufferedData(). Soo, anyone have a clue as to how this should be done?? Any examples? Becuase the Help doesn't...well, help! :confused:

      1 Reply Last reply
      0
      • M mikasa

        Ok, in no way am I a N00b at reading files! However, I was debugging some code today in which I was using a IO.StreamReader to read the text from a file and using an IO.StreamWriter to write it into a different file. This file is about 3.2MB. The StreamReader DOES return the Correct Length of the File (IO.StreamReader.BaseStream.Length). HOWEVER, while reading the file, the Stream THINKS it has reached the end, and like a good little programmer, I Flush the StreamWriter (BTW, I am flushing every 8096 Bytes - yes, that IS a lot of water!) but I am missing a HUGE chunk of data at the End of my file! I have noticed that the IO.StreamReader.BaseStream.Position is usually TOTALLY different than the Sum of the Bytes returned for each re-iteration of retrieving data. For example, reading in 8096 Bytes at a time, when my Position variable thinks it's at 16,192, the BaseStream has it's internal buffer set to some other amount... Is this what's messing it up? Notice however, it I REALLY want to waste a lot of memory and processing time, I can just read it all in at once (IO.StreamReader.ReadToEnd) and then process it....GreeeEEEAAAaaaat!!! Just what I want! NOT!!! So...what the @%$* is going on!??? How do I read the rest of the data!??? :wtf:

        M Offline
        M Offline
        mikasa
        wrote on last edited by
        #3

        Ok, I found the answer to this. I'm sure most of you do not need this functionality and only use "ReadLine"... However, try parsing ANSI 835 files ;P . (Yes, I'm aware there are 3rd party Libraries for this, but I hate them) - The End ;P

        T 1 Reply Last reply
        0
        • M mikasa

          Ok, I found the answer to this. I'm sure most of you do not need this functionality and only use "ReadLine"... However, try parsing ANSI 835 files ;P . (Yes, I'm aware there are 3rd party Libraries for this, but I hate them) - The End ;P

          T Offline
          T Offline
          TigerNinja_
          wrote on last edited by
          #4

          mikasa wrote: Ok, I found the answer So can you kindly share your solution with the CPians ? Thanks.

          R.Bischoff

          Denn Gott hat die Menschen so sehr geliebt, daß er seinen einzigen Sohn für sie hergab. Jeder, der an ihn glaubt, wird nicht verlorengehen, sondern das ewige Leben haben

          M 1 Reply Last reply
          0
          • T TigerNinja_

            mikasa wrote: Ok, I found the answer So can you kindly share your solution with the CPians ? Thanks.

            R.Bischoff

            Denn Gott hat die Menschen so sehr geliebt, daß er seinen einzigen Sohn für sie hergab. Jeder, der an ihn glaubt, wird nicht verlorengehen, sondern das ewige Leben haben

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

            Sure thing, just curious if anyone would reply ;P

            'Function to Read a File into Cache so RegEx can be Performed
            Private Sub CacheFile(ByVal File As String)
            Dim ioReader As IO.StreamReader
            Dim vBuffer() As Char
            Dim hResult As Integer

            'Exit if the File does not exist
            If (File = "") Then Return
            If (Not IO.File.Exists(File)) Then Return
            If (Me.\_File <> File) Then Me.\_File = File Else Return
            
            Try
            	'Initialize the Cache
            	\_Cache = Nothing : \_Cache = New System.Text.StringBuilder(8096)
            
            	'Read the File
            	ReDim vBuffer(8096)
            	ioReader = New IO.StreamReader(File, System.Text.Encoding.UTF8, True, 8096)
            	hResult = ioReader.Read(vBuffer, 0, vBuffer.Length) 'Read More Data
            	While (hResult > 0)
            	     'There is a bug with a StreamReader.  Sometimes more data is read than is neccessary.
            	     'If these are the Last Bytes of data, only read up to the hResult
            	     If (hResult < vBuffer.Length) Then \_Cache.Append(vBuffer, 0, hResult) Else \_Cache.Append(vBuffer, 0, vBuffer.Length)
            
            	     hResult = ioReader.Read(vBuffer, 0, vBuffer.Length) 'Read More Data
            	End While
            
            Catch ex As Exception : MsgBox(ex.ToString)
            Finally
            	Erase vBuffer
            	If (Not IsNothing(ioReader)) Then ioReader.Close() : ioReader = Nothing
            End Try
            

            End Sub

            C 1 Reply Last reply
            0
            • M mikasa

              Sure thing, just curious if anyone would reply ;P

              'Function to Read a File into Cache so RegEx can be Performed
              Private Sub CacheFile(ByVal File As String)
              Dim ioReader As IO.StreamReader
              Dim vBuffer() As Char
              Dim hResult As Integer

              'Exit if the File does not exist
              If (File = "") Then Return
              If (Not IO.File.Exists(File)) Then Return
              If (Me.\_File <> File) Then Me.\_File = File Else Return
              
              Try
              	'Initialize the Cache
              	\_Cache = Nothing : \_Cache = New System.Text.StringBuilder(8096)
              
              	'Read the File
              	ReDim vBuffer(8096)
              	ioReader = New IO.StreamReader(File, System.Text.Encoding.UTF8, True, 8096)
              	hResult = ioReader.Read(vBuffer, 0, vBuffer.Length) 'Read More Data
              	While (hResult > 0)
              	     'There is a bug with a StreamReader.  Sometimes more data is read than is neccessary.
              	     'If these are the Last Bytes of data, only read up to the hResult
              	     If (hResult < vBuffer.Length) Then \_Cache.Append(vBuffer, 0, hResult) Else \_Cache.Append(vBuffer, 0, vBuffer.Length)
              
              	     hResult = ioReader.Read(vBuffer, 0, vBuffer.Length) 'Read More Data
              	End While
              
              Catch ex As Exception : MsgBox(ex.ToString)
              Finally
              	Erase vBuffer
              	If (Not IsNothing(ioReader)) Then ioReader.Close() : ioReader = Nothing
              End Try
              

              End Sub

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              In your code you have:

              _Cache = Nothing : _Cache = New System.Text.StringBuilder(8096)

              Isn't the first statement somewhat redundant?


              "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

              M 1 Reply Last reply
              0
              • C Colin Angus Mackay

                In your code you have:

                _Cache = Nothing : _Cache = New System.Text.StringBuilder(8096)

                Isn't the first statement somewhat redundant?


                "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!

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

                I don't trust VB nor the Garbage Collector ;P

                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