Duplicate save in txt file

 

Hi,

I don't want to save duplicate file in txt file.

If I have EURUSD;TYPEORDER=0

Before save I need to check if already exists.


This is my code:

void SaveStringToFile()
  {
for(int i=OrdersTotal()-1; i>=0; i--)
     {
     
     string text = "Symbol:"+OrderSymbol()+";OpenTradeDate:"+OrderOpenTime()+";TradeType:"+OrderType()+";MagicNumber:"+OrderMagicNumber();
     
   int handle;
  handle=FileOpen (InpDirectoryName + "//" + InpFileName, FILE_TXT|FILE_WRITE|FILE_READ, ';');
  if(handle>0)
    {if(OrderSelect(i,SELECT_BY_POS))
     FileSeek(handle, 0, SEEK_END);
     FileWrite(handle, text);
     FileClose(handle);
    }
  }
  } 
 //string filename = InpDirectoryName + "//" + "ExportRealTimeData.txt"
 int handle=FileOpen(InpDirectoryName + "//" + "ExportRealTimeData.txt", FILE_TXT|FILE_READ|FILE_SHARE_READ|FILE_ANSI,",");
  if (handle < 1)  
    Print("File " + InpDirectoryName + "//" + "ExportRealTimeData.txt" + " not found.");
  else  {
 while(!FileIsEnding(handle))

      text+=FileReadString(handle,intSize);
      
    //  if (
      
   //   SaveStringToFile();
     }
     
     if (!StringFind(text,"Symbol:EURUSD;OpenTradeDate:2022.01.21 11:27:32;TradeType:1;MagicNumber:0"))
     {
      checkText=true;
     
       //  SaveStringToFile(); //Print("3");
     }
     else
     checkText=false;
     
  //  Print(text);
    FileClose(handle);
  }      
      
      

      
      
      
      if (checkText==false)
      {
         SaveStringToFile();
      }
      

        
      if (checkText==true)
      {
         Print("true");
      }

    
 

FileIsExist()

 
  1. Your code
    if (!StringFind(text,"Symbol:EURUSD;OpenTradeDate:2022.01.21 11:27:32;TradeType:1;MagicNumber:0"))
      {
          checkText=true;
      }
    else
         checkText=false;
    Simplified
    checkText= !StringFind(text,"Symbol:EURUSD;OpenTradeDate:2022.01.21 11:27:32;TradeType:1;MagicNumber:0");
    
              Increase Order after stoploss - MQL4 programming forum #1.3 (2017)

  2. Dejan Krapez:


    I don't want to save duplicate file in txt file.

    Before save I need to check if already exists.

    What is your question? If the file exists, or if the line exists?
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  3.       text+=FileReadString(handle,intSize);
    What is intSize and why are you only reading part of each line?