Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

File.AppendText causing severe hair loss

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    File.AppendText causing severe hair loss

    Below is a section of code that writes to a file just fine so long as the else statement is remarked out. If it is left in, all I get is a 0 byte file. What am I doing wrong?

    Thanks a mil


    try
    {
    if(dExp.WCCIPattern[0] != 0)
    {
    sw = File.AppendText(@FilePath);
    // If file at 'path' doesn't exist it will create the file. If it does exist it will append the file.
    if(dExp.WCCIDirection[0] == 1)
    nextOpen = Close[0] + TickSize;
    else
    nextOpen = Close[0] - TickSize;

    sw.Write("\r\n"+Time[0].ToString("M-dd-yyy")+","+Time[0].ToString("HH:mm")+","+Pattern+","+dExp.WCCIDirect ion+","+","+nextOpen);
    sw.Flush();
    sw.Close();
    }
    else
    if(dExp.WCCIPattern[1] != 0)
    {
    sw = File.AppendText(@FilePath);
    if(dExp.WCCIDirection[1] == 1)
    {
    sw.Write(","+(Open[0] - Low[0])/TickSize+","+(High[0]-Open[0])/TickSize);
    sw.Flush();
    sw.Close();
    }
    else
    {
    sw.Write(","+(High[0] - Open[0])/TickSize+","+(Open[0]-Low[0])/TickSize);
    sw.Flush();
    sw.Close();
    }
    }
    }
    catch (Exception e)
    {
    // Outputs the error to the log
    Log("You cannot write and read from the same file at the same time. Please remove SampleStreamReader.", NinjaTrader.Cbi.LogLevel.Error);
    throw;
    }

    #2
    Shouldn't that be "else if (XXX)" since it looks like you have

    if ()
    {}
    else
    {}
    else
    {}
    RayNinjaTrader Customer Service

    Comment


      #3
      Found the problem. It was that blasted [1] look back. Grrrrrrrrrrrrrrrrrrrr.

      Comment


        #4
        Could someone please show me how to write it out so that FIRST it wipes out any data in the file, and then appends all the new data into it? Been on the C+ zone but just can't find this out.

        Comment


          #5
          Unfortunately I have no sample for this case available cclsys, couldn't you use FileAppendAllText, to create new file each and then point to it?

          Provides static methods for the creation, copying, deletion, moving, and opening of a single file, and aids in the creation of FileStream objects.

          Comment


            #6
            I'm far from an expert and am kinda guessing here, but using the File.AppendText(@FilePath) to add data, it would seem that File.Delete(@<filename>) (or something like it) would delete the file.

            See http://www.csharpfriends.com/Article...x?articleID=54

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            558 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            324 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            545 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            547 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X