i'm having issue using memory stream on large text file function in c#. output text file doesn't contain information, it's missing last few lines of text wrote in streamwriter.
many of posts use stream.flush() , reset stream beginning of stream using stream.seek(0, seekorigin.end), neither seem make difference.
here code:
using (var stream = new memorystream()) { using (var sb = new streamwriter(stream)) { decimal[,] oparray = new decimal[477, 521]; list<rech_rch_result> result1; //rch file header sb.writeline("#modflow2000 recharge package"); sb.writeline("parameter 0"); sb.writeline(" 1 50"); (int sp = 1; sp < 3; sp++) //should 209 { objectresult<rech_rch_result> qryrch = db.rech_rch(nrd, sp); result1 = qryrch.select(p => new rech_rch_result { m_col = p.m_col, m_row = p.m_row, recharge = p.recharge, stressperiod = p.stressperiod }).tolist(); //stress period header sp sb.writeline(" 1 1"); sb.writeline(" 18 1.000(10e15.6) -1 recharge"); //model row = 1 476, col = 1 520 foreach (rech_rch_result s in result1) { oparray[s.m_row.value, s.m_col.value] = s.recharge; } (int x = 1; x < 477; x++) { (int y = 1; y <= 520; y += 10) { //write out values in chunks of 10 sb.writeline(string.format(" {0:0.000000e+00} {1:0.000000e+00} {2:0.000000e+00} {3:0.000000e+00} {4:0.000000e+00} {5:0.000000e+00} {6:0.000000e+00} {7:0.000000e+00} {8:0.000000e+00} {9:0.000000e+00}", oparray[x, y], oparray[x, y + 1], oparray[x, y + 2], oparray[x, y + 3], oparray[x, y + 4], oparray[x, y + 5], oparray[x, y + 6], oparray[x, y + 7], oparray[x, y + 8], oparray[x, y + 9])); } } array.clear(oparray, 0, oparray.length); } stream.flush(); //looks stream not completing , missing few lines. //stream.seek(0, seekorigin.begin); stream.seek(0, seekorigin.end); using (filestream file = system.io.file.create(server.mappath(string.format("~/rchfiles/{0}.txt", filename)))) { stream.writeto(file); } //using (zipfile zip = new zipfile()) //{ // zip.addentry(filename + ".rch", stream); // zip.save(server.mappath(string.format("~/rchfiles/{0}.zip", filename))); //} } }
you're flushing memorystream, not streamwriter. it's possible that's buffering.
call sb.flush() instead of stream.flush(). (flushing memorystream doesn't anyway.)
also note can use memorystream.copyto without rewinding, instead of stream.writeto.
Comments
Post a Comment