Hello,
I wanted to know why i get a OutOfMemory Exception in the following:
basicly I have a textbox/string with ~ 90.000.000 characters (or more)
And what I want do is writing them to a file..
BinaryWriter writer = new BinaryWriter(new FileStream(AppDomain.CurrentDomain.BaseDirectory + "test.txt", FileMode.Create, FileAccess.Write));
writer.Write(textBox1.Text);
writer.Close();
using (StreamWriter outfile = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "test.txt"))
{
outfile.Write(textBox1.Text);
}
So on this way I get the memory exceptions.
If I try to reduce the size by splitting the main string into
subs ..
if (textBox1.TextLength >= 28000000)
{
int step_size = 28000000;
int step_count = textBox1.TextLength / step_size;
int cur_begin = 0;
int cur_end = step_size;
textBox4.AppendText("TextBoxCharacterCount: " + textBox1.TextLength.ToString() + Environment.NewLine);
for (int i = 0; i < step_count; i++)
{
string sub = textBox1.Text.Substring(cur_begin, cur_end);
if (i == 0)
{
BinaryWriter writer = new BinaryWriter(new FileStream(AppDomain.CurrentDomain.BaseDirectory + textBox2.Text + ".h", FileMode.Create, FileAccess.Write));
writer.Write(sub);
writer.Close();
}
else
{
BinaryWriter writer = new BinaryWriter(new FileStream(AppDomain.CurrentDomain.BaseDirectory + textBox2.Text + ".h", FileMode.Open, FileAccess.Write));
writer.Write(sub);
writer.Close();
}
cur_begin = cur_end + 1;
cur_end = cur_end + step_size - 1;
}
string end = textBox1.Text.Substring(cur_begin, textBox1.TextLength);
BinaryWriter end_writer = new BinaryWriter(new FileStream(AppDomain.CurrentDomain.BaseDirectory + textBox2.Text + ".h", FileMode.Open, FileAccess.Write));
end_writer.Write(end);
end_writer.Close();
}
else
{
BinaryWriter writer = new BinaryWriter(new FileStream(AppDomain.CurrentDomain.BaseDirectory + textBox2.Text + ".h", FileMode.Create, FileAccess.Write));
writer.Write(textBox1.Text);
writer.Close();
}
I still get the memory exception at sub this time.
But why ? a string can contain 2.147.483.647 characters in c#
and the current size would be far below, means ~ 28.000.000
And the most important fact is, that if i want write a string with
~ 29.000.000 with the very first method, it works , but if i try to split
90.000.000 into 28.000.000 parts it doesnt work at all ...
,greetings
What I have tried:
destribes above in the related question :)