Announcement

Collapse
No announcement yet.

Error reading from .txt file

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

  • Error reading from .txt file

    So I'm trying to read dates from a flat .txt file in a windows environment.

    Here is my file:

    2017.03.17 20:00,X
    2017.03.17 20:01,X
    2017.03.17 22:05,X

    Here is my code:
    int h = File.Open("trade_times_.txt",FILE_READ,1);
    for (int i = 0; i <= 2; i++)
    {
    string t1 = FileReadString(h);
    string sep=","; // A separator as a character
    ushort u_sep; // The code of the separator character
    string in_parms[]; // An array to get strings
    u_sep=StringGetCharacter(sep,0);
    StringSplit(t1,u_sep,in_parms);

    Print(in_parms[0]);
    MqlDateTime time_2;
    TimeToStruct(in_parms[0],time_2);
    Print(time_2.hour + ":" + time_2.min);
    }

    Here is the output (hour and minute are correct for the first line but not for the next two:

    2017.03.16 22:11:32.028 test (ENQM7,M1) 2017.03.17 20:00
    2017.03.16 22:11:32.028 test (ENQM7,M1) 20:0
    2017.03.16 22:11:32.028 test (ENQM7,M1) 2017.03.17 20:01
    2017.03.16 22:11:32.028 test (ENQM7,M1) 0:0
    2017.03.16 22:11:32.028 test (ENQM7,M1) 2017.03.17 22:05
    2017.03.16 22:11:32.028 test (ENQM7,M1) 0:0

    Thanks.

  • #2
    Code:
       int h=FileOpen("trade_times_.txt",FILE_READ,1);
       for(int i=0; i<=2; i++)
         {
          string t1=FileReadString(h);
          string in_parms[];
          int parts=StringSplit(t1,',',in_parms);
          if(parts>0)
            {
             datetime dt=StringToTime(in_parms[0]);
             MqlDateTime time_2;
             TimeToStruct(dt,time_2);
             printf("%02d:%02d",time_2.hour,time_2.min);
            }
         }

    Comment


    • #3
      Thanks, but there's something wrong with the encoding. Once more:

      Here is the file: trade_times.txt:
      2017
      2017
      2017
      2017

      Here is the code:
      string symbol_names[];

      string ExtFileName= "trade_times.txt";
      int fh = File.Open(ExtFileName,FILE_READ,9);

      FileReadArray(fh, symbol_names,0, WHOLE_ARRAY);
      File.Close();

      for(int i=0;i<ArraySize(symbol_names);i++)
      {
      Print(symbol_names[i] + " " + StringLen(symbol_names[i]));
      }

      Here is the output:

      2017.03.17 07:35:21.923 test3 (ENQM7,M1) 2017 4
      2017.03.17 07:35:21.923 test3 (ENQM7,M1) 2017 5
      2017.03.17 07:35:21.923 test3 (ENQM7,M1) 2017 4
      2017.03.17 07:35:21.923 test3 (ENQM7,M1) 2017 5

      The same lines are variously being read with length 4 and 5.

      I think it's something with the fileOpen flag, but what?

      Comment


      • #4
        TXT File:
        2017
        2017
        2017
        2017
        Code:
           int fh=FileOpen("trade_times_.txt",FILE_READ|FILE_TXT);
           string symbol_names[];
           FileReadArray(fh,symbol_names);
           FileClose(fh);
           for(int i=0;i<ArraySize(symbol_names);i++)
             {
              printf("%s %d",symbol_names[i],StringLen(symbol_names[i]));
             }
        Result:
        2017 4
        2017 4
        2017 4
        2017 4
        Last edited by avaticks; 03-17-2017, 07:15 AM.

        Comment


        • #5
          Originally posted by avaticks View Post
          TXT File:


          Code:
          int fh=FileOpen("trade_times_.txt",FILE_READ|FILE_TXT);
          string symbol_names[];
          FileReadArray(fh,symbol_names);
          FileClose(fh);
          for(int i=0;i<ArraySize(symbol_names);i++)
          {
          printf("%s %d",symbol_names[i],StringLen(symbol_names[i]));
          }
          Result:

          Avaticks, So when I paste your code into the OnStart of my script, my output looks like this !!

          2017.03.17 09:18:10.165 test3 (ENQM7,M1) 〲㜱〮⸳㜱㈠㨰〰䈤奕ㄠ䔠兎㝍䴠呋ㄠ〰ㄠ〰਍㈍㄰⸷㌰ㄮ‷〲〺␰䱃协⁅久䵑ഷ㈊㄰⸷㌰ㄮ‷〲〺␰啂⁙‱久䵑 ‷䭍⁔〱‰〱രഊ〲㜱〮⸳㜱㈠㨰〰䌤佌䕓䔠兎㝍਍〲㜱〮⸳㜱㈠㨰〰䈤奕ㄠ䔠兎㝍䴠呋ㄠ〰ㄠ〰਍㈍㄰⸷㌰ㄮ‷ 〲〺␰䱃协⁅久䵑ഷ㈊㄰⸷㌰ㄮ‷〲〺␰啂⁙‱久䵑‷䭍⁔〱‰〱രഊ〲㜱〮⸳㜱㈠㨰〰䌤佌䕓䔠兎㝍਍ 146

          ?????

          Comment


          • #6
            I think you are using UNCODE file format. Convert to ANSI and all be fine.

            Comment


            • futures_track
              futures_track commented
              Editing a comment
              Thanks for your patience. You are right. Here is the correct FileOpen():

              int fh=FileOpen("test_.txt",FILE_READ|FILE_TXT|FILE_AN SI);

          Disclaimer: There is a risk of loss in trading futures, forex and options. Futures, forex and options trading are not appropriate for all investors. Only risk capital should be used when trading futures. All information is for educational use only and is not investment advice. Past performance is not indicative of future results.

          This website is hosted and operated by AMP Global Clearing, LLC ("AMP"), which provides brokerage services to traders of futures and foreign exchange products. This website is intended for customer support, educational and informational purposes only and should NOT be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by AMP and the information made available on this Web site is NOT an offer or solicitation of any kind. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of AMP.

          All Users and Contributors along with their websites, products and services, are independent persons or companies that are in no manner affiliated with AMP. AMP is not responsible for, do not approve, recommend or endorse any User and/or Contributor Content referenced on this website and it’s your sole responsibility to evaluate All Content. Please be aware that any performance information provided by a user and/or contributor should be considered hypothetical.

          HYPOTHETICAL PERFORMANCE RESULTS HAVE MANY INHERENT LIMITATIONS, SOME OF WHICH ARE DESCRIBED BELOW. NO REPRESENTATION IS BEING MADE THAT ANY ACCOUNT WILL OR IS LIKELY TO ACHIEVE PROFITS OR LOSSES SIMILAR TO THOSE SHOWN. IN FACT, THERE ARE FREQUENTLY SHARP DIFFERENCES BETWEEN HYPOTHETICAL PERFORMANCE RESULTS AND THE ACTUAL RESULTS SUBSEQUENTLY ACHIEVED BY ANY PARTICULAR TRADING PROGRAM.

          ONE OF THE LIMITATIONS OF HYPOTHETICAL PERFORMANCE RESULTS IS THAT THEY ARE GENERALLY PREPARED WITH THE BENEFIT OF HINDSIGHT. IN ADDITION, HYPOTHETICAL TRADING DOES NOT INVOLVE FINANCIAL RISK, AND NO HYPOTHETICAL TRADING RECORD CAN COMPLETELY ACCOUNT FOR THE IMPACT OF FINANCIAL RISK IN ACTUAL TRADING. FOR EXAMPLE, THE ABILITY TO WITHSTAND LOSSES OR TO ADHERE TO A PARTICULAR TRADING PROGRAM IN SPITE OF TRADING LOSSES ARE MATERIAL POINTS WHICH CAN ALSO ADVERSELY AFFECT ACTUAL TRADING RESULTS. THERE ARE NUMEROUS OTHER FACTORS RELATED TO THE MARKETS IN GENERAL OR TO THE IMPLEMENTATION OF ANY SPECIFIC TRADING PROGRAM WHICH CANNOT BE FULLY ACCOUNTED FOR IN THE PREPARATION OF HYPOTHETICAL PERFORMANCE RESULTS AND ALL OF WHICH CAN ADVERSELY AFFECT ACTUAL TRADING RESULTS.

          AMP has NOT endorsed or recommended the use any product, services offered by any 3rd party User or Contributor on this forum. No person employed by, or associated with, AMP is authorized to provide any information about any 3rd party User or Contributor content. All specific brokerage related questions should be directed towards AMP.

          Copyright © 2017. All rights reserved.

          Working...
          X