The players…
I have 2 files, File1.txt is 9.6mb, File2.txt is 42k. Both are just regular text files with no special characters other than the usual \r\n at the end of each line. I also have a Win Form with 2 buttons that have the code below attached.
The puzzle…
Button1 reads in File1 via a StreamReader and works perfectly.
Button2 reads in File2 via a StreamReader and does not work.
The code…
private void button1_Click(object sender, EventArgs e)
{
SortedDictionary<string, string>[] SD;
StreamReader sr = new StreamReader("file1.txt", System.Text.Encoding.UTF8);
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
char[] buffer = new char[10 * 1024 * 1024];
int xx = sr.ReadBlock(buffer, 0, buffer.Length);
sb.Append(buffer);
}
SD = Parser.ParseFile1(sb.ToString());
//do something with it
--snip--
}
private void button2_Click(object sender, EventArgs e)
{
SortedDictionary<string, string>[] SD;
StreamReader sr = new StreamReader("file2.txt", System.Text.Encoding.UTF8);
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
char[] buffer = new char[10 * 1024 * 1024];
int xx = sr.ReadBlock(buffer, 0, buffer.Length);
sb.Append(buffer);
}
SD = Parser.ParseFile2(sb.ToString());
//do something with it
--snip--
}
If I click Button1, the code runs as expected. The file is read in, dumped into the StringBuilder and then into the ParseFile1 routine as a string. Life is good.
If I click Button2, the file appears to be read in ok, I can examine the contents of the StringBuilder and the entire file is there, but once I hit the ToString() method on the StringBuilder and try to examine the contents of the string, I get a “serious error in the debugger” with no other information, and have to restart Visual Studio.
What’s the problem?
The answer…
I did eventually figure it out. This one wasn’t obvious (to me, at least) so I’m hoping it stumps at least a few of you.
Select the text to read the answer.
I’m not sure why, but the buffer size for the StreamReader seems to matter. For File2, the buffer was way too big at 10MB (10 * 1024 * 1024) so I changed it to 100K (100 * 1024) and that did the trick. Now, can someone tell me why this matters? |
UPDATE:
After getting a fair amount of feedback on this post, and some suggestions of different code to try, I’m happy to say the “problem” is no longer a problem, and the code is MUCH cleaner as a result.
Instead of:
SortedDictionary<string, string>[] SD;
StreamReader sr = new StreamReader("file2.txt", System.Text.Encoding.UTF8);
StringBuilder sb = new StringBuilder();
while (!sr.EndOfStream)
{
char[] buffer = new char[10 * 1024 * 1024];
int xx = sr.ReadBlock(buffer, 0, buffer.Length);
sb.Append(buffer);
}
SD = Parser.ParseFile2(sb.ToString());
I now have:
SortedDictionary<string, string>[] SD = Parser.ParseFile2(File.ReadAllText("file2.txt", Encoding.UTF8));
No more memory errors, nice and clean. Thanks guys!! (I’m a little embarrassed to admit that I didn’t know File.ReadAllText existed. I would have certainly started with that, had I known.)