Announcement

Collapse
No announcement yet.

Output to a file

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Output to a file

    What is the proper code to write comments to a file.

    The following doesnt seem to write anything, but does create a file with the proper name in the proper location.

    var f1; //used for the output file
    var Log1;

    function main()
    {
    today =new Date;
    CurMonth =today.getMonth() +1;
    CurDate =today.getDate() *1;
    CurDay =today.getDay() *1;

    // THE TRADE LOG
    //var Log1;

    // FILENAME WILL LOOK LIKE ES-MM-DD-2003.txt
    Log1 = "ES-" + CurMonth + "-" + CurDate + "-2003.txt";

    f1 = new File(Log1); // PUT IT IN FORMULA OUTPUT
    f1.open("at+"); // I have no idea what this line does

    *if(!f1.isOpen())
    {
    debugPrintln("*** Couldn't open TRADE LOG!");
    }
    else
    {
    debugPrintln("*** ES TRADE LOG OPEN");
    }

    // do a bunch of stuff here

    //f1.writeln("ES SHORT REVERSAL, Cover");

    // CLOSING THE LOG FILES
    if(f1.isOpen())
    {f1.close();}

    return;

  • #2
    What happens if you open it ("wt+"); rather than ("at+")?

    I have noticed an oddity that trying to append to an empty file doesn't seem to work...

    In theory You can then use setPosition to position to the end of a file after opening with "wt+"...

    G
    Garth

    Comment


    • #3
      where can I read up on these commands such at at+ and wt+

      I am clueless on this stuff - right now anyway, hehe

      Comment


      • #4
        Here is one place:



        Look under "File".

        I'm sure there are other JavaScript sites that would also have this info...
        Garth

        Comment


        • #5
          ah ha... don't see at+ though, just a and a+

          The possible values for mode are as follows:


          r[b] opens a file for reading. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false.

          w[b] opens a file for writing. If the file does not already exist, it is created; otherwise, it is overwritten. This method always succeeds and returns true.

          a[b] opens a file for appending (writing at the end of the file). If the file does not already exist, it is created. This method always succeeds and returns true.

          r+[b] opens a file for reading and writing. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false. Reading and writing commence at the beginning of the file. When writing, characters at the beginning of the file are overwritten.

          w+[b] opens a file for reading and writing. If the file does not already exist, it is created; otherwise, it is overwritten. This method always succeeds and returns true.

          a+[b] opens a file for reading and appending. If the file does not already exist, it is created. This method always succeeds and returns true. Reading and appending commence at the end of the file.

          Comment


          • #6
            is the [b] syntax used for a binary file?

            Comment


            • #7
              David,

              You ask a good question. I have always assumed that the "wt" and "at" means text and to open a binary you would do "wb" and "ab"...

              But this is an assumption on my part.
              Garth

              Comment


              • #8
                Re: Reply to post 'Output to a file'

                I'll get the info on these flags and post it here shortly.
                m.

                --- [email protected] wrote:
                > Hello mattgundersen,
                >
                > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                >
                Matt Gundersen

                Comment


                • #9
                  Can any tell why this writes to the file during efs reload, but doesnt as the ticks go by. thx.

                  setPriceStudy(true);
                  var f1;
                  debugClear();
                  // THE TRADE LOG
                  var Log1;
                  function preMain()
                  {
                  today =new Date;
                  var my_day = today.getDate();
                  var my_month = today.getMonth() + 1;
                  var my_year = today.getFullYear();

                  // FILENAME WILL LOOK LIKE ES-MM-DD-YYYY.txt
                  Log1 = "SsS-" + my_month + "-" + my_day + "-" + my_year+".txt";
                  f1 = new File(Log1); // PUT IT IN FORMULA OUTPUT
                  f1.open("w+t");
                  }

                  function main()
                  {
                  //if(getCurrentBarIndex()==0)
                  //{

                  if(!f1.isOpen())
                  {debugPrintln("*** Couldn't open TRADE LOG!");}
                  else
                  {debugPrintln("*** TRADE LOG opened!");}

                  f1.write("This is the first line ");//no crlf
                  f1.writeln(" and this is appended to the first line");//uses crlf

                  f1.write("This is the second line");
                  f1.writeln(" and this is appended to the second line");

                  curtime();
                  //f1.writeln(CurHours+"."+CurMinutes+"."+CurSeconds) ;

                  // CLOSING THE LOG FILES
                  //if(f1.isOpen())
                  //{f1.close();}

                  return;
                  }
                  //}

                  function curtime()
                  {
                  today =new Date;
                  CurHours =today.getHours();
                  CurMinutes=today.getMinutes();
                  CurSeconds=today.getSeconds();
                  if(CurHours <10){CurHours ="0"+CurHours ;}
                  if(CurMinutes<10){CurMinutes="0"+CurMinutes;}
                  if(CurSeconds<10){CurSeconds="0"+CurSeconds;}
                  debugPrintln(CurHours+"."+CurMinutes+"."+CurSecond s);
                  f1.writeln(CurHours+"."+CurMinutes+"."+CurSeconds) ;
                  return;
                  }

                  Comment


                  • #10
                    I can't. Is there any possibility that the file position is being reset to SOF each time main is calledand you are really writing the same information on top of itself for each tick?

                    You might try adding some logic that increments a counter each time main() is called and then if the counter == 1 print more data to the file, if its == 2 print even more etc.

                    This will tell you if you are only writing once, or if you are constantly overwriting.
                    Garth

                    Comment


                    • #11
                      I just ran it and it output on every tick. Even uncommented the

                      if(getCurrentBarIndex() == 0)

                      line and it still worked. My only guess is that the global Compute On Close is turned on in the EFS Settings?

                      m.
                      Matt Gundersen

                      Comment


                      • #12
                        Info on the open flags

                        Here's the info on the open flags

                        "r"

                        Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

                        "w"

                        Opens an empty file for writing. If the given file exists, its contents are destroyed.

                        "a"

                        Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

                        "r+"

                        Opens for both reading and writing. (The file must exist.)

                        "w+"

                        Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

                        "a+"

                        Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.


                        In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:

                        t

                        Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.

                        Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

                        b

                        Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

                        If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.
                        Matt Gundersen

                        Comment

                        Working...
                        X