System.IO.StreamWriter Data Corrpution
I have a problem with an application I have written. To keep it simple, it does heavy math calculations. If the results meet a specific criteria, it is written to a text file on a line-line-basis.
The problems is, if I go look at the data files, I will find non-numeric symbols mixed in with the numbers. Sometimes it is a question mark, "?". Other times, it is control or extended characters that Notepad or Wordpad cannot display. They can be mixed into the numbers, like so: 215x7 or 8948x, where "x" is the odd character. I thought the problem may have been my use of "DataFile.Flush". I removed that, but still have the corruption.
Below are my declarations and code fragments:
Code:
Dim Number as ULong
Dim DataFile As System.IO.StreamWriter
DataFile = My.Computer.FileSystem.OpenTextFileWriter(FilePath & fName, False)
DataFile.WriteLine(Number)
The first time I had a problem, I thought it was my use of "Cstr()" in the WriteLine statement, with "Flush" as I stated above.
I do not know if this is a situation internal to VB2008, or external, like hard drive buffers writing.
:confused:
Re: System.IO.StreamWriter Data Corrpution
I have never seen anything like that, and would expect to have seen it. Therefore, I think you should take a harder look at the output. That may not be easy to do at all, but here are a couple thoughts that come first to mind:
1) Try using a memory stream rather than a file, so that you can take a look at the bytes that are being written. This is a simple, and almost certainly useless, test.
2) Try Number.ToString. That shouldn't make any difference at all, but it sure would be interesting if it did.
3) Have you tried reading the file back in, rather than looking in Notepad? If it reads it back in, and does so correctly, then the problem may not be in the writing. If it fails to read back what was written....that would be confirmation.
Re: System.IO.StreamWriter Data Corrpution
I found something that may have a bearing. In the help documentation, I found references to methods that format output to the specified type:
Code:
WriteLine(UInt64)
<CLSCompliantAttribute(False)> _
Public Overridable Sub WriteLine (value As ULong)
Dim instance As TextWriter
Dim value As ULong
instance.WriteLine(value)
The "CLSCompliantAttribute" in this case is a link to another page, which contains links to other pages, and on and on. I hate that! The examples on how to use this are less than poor...
Re: System.IO.StreamWriter Data Corrpution
I think I've tossed out over 250 hours of running for nothing. Below is a section of a data file showing the strange character.
Quote:
10072079779
10072079833
皜10072079851
10072079869
10072079887
Using a hex editor, I found the number following the symbol. Below is a hex dump of that section:
Quote:
30 37 32 30 37 39 37 37 39 0d 0a 31 30 30 37 32 30 37 39 38 33 33 0d 0a 31 30 30 37 32 30 37 39 38 35 31 0d 0a 31 30 30 37 32 30 37 39 38 36 39 0d 0a 31 30 30 37 32 30 37 39 38 38 37 0d 0a 31
And below is the part of the dump which is showing the character and the number.
Quote:
0d 0a 31 30 30 37 32 30 37 39 38 35 31 0d 0a
There is no hex value for a control character here other than the "0d 0a" at each end. Those are for line separation.; what used to be known as "line-feed" and "carriage-return." The odd character seems to appear between "0a" and "31", but not in the hex view.
I think I'm going nuts...
:lol:
Re: System.IO.StreamWriter Data Corrpution
Yeah, that is always an option. So it appears that this is not in the output, but has to do with the reading in the program.
You mentioned both Notepad and Wordpad, but did you actually try both of them? They have different ways of displaying similar data.
Re: System.IO.StreamWriter Data Corrpution
I output my data specifically to be viewed with Notepad or Wordpad. It is also machine readable using a line-input type of statement.
Quote:
2123829731
2123829739
2123829781
2123829797
2123829847
2123皜29857
2123829881
2123829901
2123829973
2123829989
2123830003
2123830063
I found another symbol inside the data in a different file. Again, with a hex editor, the symbol cannot be found. So, it seems to be a read error by Wordpad. On this particular file, I tried viewing it with Notepad. No symbol.
For some reason, Wordpad seems to change fonts. It is always after the font change that I see the symbol, and always very near the end of the file.
Well, I'm not going to worry about this any longer. It's a shame I lost so much time.
Re: System.IO.StreamWriter Data Corrpution
I would have guessed that you saw the character in Notepad, but not in Wordpad. Interesting that it was the other way around. I have seen something vaguely like this, in that Notepad and Wordpad showed the file differently, but in that case, I don't remember seeing characters.