Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 495
  • Last Modified:

FileStream.Length question.

Hi, I'm using VB.NET2010 (VS2010), WinForms. I have a TXT file which I'm reading into a FileStream and then using StreamReader to process the file.

I'm experiencing a unique issue with the FileStream.Length property, which is giving me a character count larger than the number of characters in the actual file.

My file contains 51314  characters. I've tested this by reading the number of characters per line in the file and summing them after each loop, while the file is being read. The sum = 51314

The FileStream.Length property returns a count of 53554 characters.

The difference = 2240 characters.  (FS.length - SUM(number of characters per line))

However, what's interesting is that the line count = 1120 lines, which are read one line at a time.

Inspecting this further;  2240/1120 = 2. So, it seems that the FileStream.Length property is adding 2 characters per line...why? Is it taking into consideration the newline character and counting that as 2 per line?

Thanks for your help in helping me understand this...

Fulano
0
Mr_Fulano
Asked:
Mr_Fulano
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Paul JacksonCommented:
Filestream.Length returns the size of the stream in bytes not characters.
0
 
Wayne Taylor (webtubbs)Commented:
Your 2 extra characters per line would be a carraige return and a line feed (VB constant vbCrLf, or vbCr + vbLf).
0
 
Paul JacksonCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mr_FulanoAuthor Commented:
Hi jacko72, yes...but in this case, its one byte per character. So, characters and bytes are the same (in this scenario). Also, StreamReader does not have a "Length" property, so I was somewhat forced to use FileStream.

Thanks,
Fulano
0
 
Mr_FulanoAuthor Commented:
Hi Webtubbs, I think your correct. However, I wish I knew what it was actually reading. I've been able to compensate in my code for the extra 2 character (bytes) by adding "2" to each line-length, but that's a haphazard way of doing it. I would much rather know what the actual number of missing characters or bytes are for each line and add that amount as opposed to guessing it will always be 2.

Thanks,
Fulano
0
 
Paul JacksonCommented:
Why not use the StreamReader.ReadLine method so then you don't need to know the number of characters.

http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx
0
 
Mr_FulanoAuthor Commented:
I actually do need to know the number of characters in each line. I use that value to calculate the percentage of the file that has been read, hence giving the user some feedback as to how far along in the process they are.

So, as for needing it, I would say I do need it.  
0
 
CodeCruiserCommented:
If the number of lines is not going to be huge, you can try

Dim Lines As String() = IO.File.ReadAllLines("path")
Dim NoOfLines As Integer = Lines.Count
Lines = Nothing
0
 
Mr_FulanoAuthor Commented:
Thanks for the help!
0
 
CodeCruiserCommented:
Glad to help :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now