CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    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.

    Code:
    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] );
                        }
                        Console.WriteLine(",");
                        outStream.WriteLine(",");
                    }
    Thanks in advance.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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:

    Code:
    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
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured