August 10th, 2012, 02:12 PM
Why is this piece of code not working
I have tried to look at it about 20 times but can't find any issue. Please bear in mind that I am a bit novice for c-sharp.
The console part works fine so my StoreArray is not having any problem but there is no output to the file.
Thanks in advance.
string tempfilename = "F:\\Research\\Out_1_TickData.csv";
outfileName = @tempfilename;
StreamWriter outStream = new StreamWriter(outfileName, true);
for (int p = 0; p < Row_StoreArray; ++p)
for (int q = 0; q < Col_StoreArray; ++q )
Console.Write("," + StoreArray[p, q]);
outStream.Write("," + StoreArray[p, q] );
August 11th, 2012, 12:23 AM
Re: Why is this piece of code not working
First - it's hard to help you debug if we don't know what you're expecting and what behavior you are observing.
General thoughts though:
(1) Call StreamWriter.Close() afterward
(2) What's with the @ in outfilename=@tempfilename. That is unusual syntax and probably unnecessary (to the extent that I wasn't even sure it was syntactically legal without checking).
My preferred method of doing this is to buffer everything first and then do one disk IO event:
System.Text.StringBuilder outData = new System.Text.StringBuilder();
for(int p; ...)
for(int q; ...)
outData.Append("your data here");
outData.AppendLine(); //New line at end of row
System.IO.File.WriteAllText(outfileName, outData.ToString()); //Write to disk
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!