Fileread not working in custom indicator.

 

The code I have for writing to a file and reading to another seems to only work with the writing to file part. I have no idea why the reading part does not work. I have made sure the file to read is not open anywhere else but still it fails. Can anyone help me out here? very perplexing.  This is in the oncalculate() function.

     

   datetime curtime = TimeCurrent();
   int filenotempty = 1;
   double fileval;
   string filestr;
   MqlDateTime timestruct;
   TimeToStruct(curtime,timestruct);
   Comment("Time in minutes: " + timestruct.min + " Time in seconds: " + timestruct.sec);
   double modmin = timestruct.min;
   double modsec = timestruct.sec;
   if (MathMod(modmin, 2.0) == 0)
   { 
      if(modsec == 1.00 || modsec == 2.0)
      {
          int h=FileOpen("PricesForLSTM.txt",FILE_WRITE|FILE_ANSI|FILE_TXT);
          if(h==INVALID_HANDLE){
          Alert("Error opening file");
          return 0;}
          FileWrite(h,"1000.00");
          
      FileClose(h);
      Alert("File created");
      while (filenotempty == 1)
      {
          int hnew = FileOpen("testnew.txt",FILE_READ|FILE_ANSI|FILE_TXT);
          if(hnew == INVALID_HANDLE) {Alert("ERROR OPENING FILE hnew"); FileClose(hnew);filenotempty = 2;}
          filestr = FileReadString(hnew);
          if (sizeof(filestr) != 0) filenotempty = 3;
      }
      if(filenotempty == 3) 
      {
          fileval = StringToDouble(filestr);
      }
      }
      
   } 
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
Other Constants - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. yugeninduce: I have no idea why the reading part does not work. I have made sure the file to read is not open anywhere else but still it fails.
    Why are you trying to close an INVALID_HANDLE?
  3. Why do you try to read if you get INVALID_HANDLE?
  4. Do you actually have a “testnew.txt?”
  5. Why is there a while loop?
  6. Where do you close the read file?
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. Why are you trying to close an INVALID_HANDLE?
  3. Why do you try to read if you get INVALID_HANDLE?
  4. Do you actually have a “testnew.txt?”
  5. Why is there a while loop?
  6. Where do you close the read file?

1: I thought thats what I did? sorry, this is my first post

2: i only started using mql yesterday, so very new. guess i dont have to do that, much apreciated!

3, 4 and 5 : yes i have such a file, it is in the correct file location (MQL5\files). the reason I have the readfile after determining if there is an error, is because if there is an handle error then it will change the while loop condition and exit the loop, if no error then the condition remains and the rest of the code gets carried out. if there is an actual string in the file then the string size is not 0. if not, it restarts the loop. the reason there is a while loop is because the write to file part is ultimately going to be the part which carries out writing the data to a file, at which point another program with an algorithm is going to read that file and output some value to the file that is being read in the second part. so if the algorithm has not output any value yet, the loop will restart until there is an output.

6. I will add that in now that you have pointed that out, thank you :)

 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. Why are you trying to close an INVALID_HANDLE?
  3. Why do you try to read if you get INVALID_HANDLE?
  4. Do you actually have a “testnew.txt?”
  5. Why is there a while loop?
  6. Where do you close the read file?

it works now as:

      while (filenotempty == false)
      {
          int hnew = FileOpen("testnew.txt",FILE_READ|FILE_ANSI|FILE_TXT);
          if(hnew == INVALID_HANDLE) {Alert("ERROR OPENING FILE hnew");break;}
          else Alert("file opened");
          filestr = FileReadString(hnew);
          if (sizeof(filestr) != 0) { filenotempty = true; Alert("file Read success"); Alert("file string is: " + filestr);}
          else Alert("file empty");
          FileClose(hnew);
      }

thanks for the input! I changed filenotempty from int to bool aswell.

Reason: