csv 从文件MQL4中删除行

nszi6y05  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(178)

我把我的EA所做的所有交易保存到一个CSV文件中。当一个交易被EA关闭时,我必须在文件中的特定行的末尾添加字符串“账面利润”。例如:下面是在交易打开时保存在文件中的行“Buy GBPJPY 146.28 145.15”,我想在上面行的末尾添加字符串“Book Profit”并将其保存到文件中。保存后该行应该看起来像“Buy GBPJPY 146.28 145.15 Book Profit”

int file_handle_dtf=FileOpen("MyTrades.CSV",FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle_dtf!=INVALID_HANDLE){
      while(!FileIsEnding(file_handle_dtf)){

         str_size1=FileReadInteger(file_handle_dtf,INT_VALUE);
         //--- read the string

         str1=FileReadString(file_handle_dtf,str_size1);
         strBP=StringConcatenate(str1,",Book Profit");
         FileWriteString(file_handle_dtf,strBP+"\n");
         }

      }

此代码只是覆盖文件,文件不可读

7qhs6swi

7qhs6swi1#

在写入文件之前,首先查找文件的结尾:

if (FileSeek(file_handle_dtf, 0, SEEK_END)) 
{
   // put file writing code here
}
ilmyapht

ilmyapht2#

使用下列函数搭配您的四个参数(Buy、GBPJPY、146.28、145.15):

void func_replaceStringInCSV(string _order,string _symbol,string _SL,string _TP)
 {
  int handle=FileOpen("MyTrades.CSV",FILE_READ|FILE_WRITE|FILE_CSV);
  if(handle!=INVALID_HANDLE)
    {
     while(!FileIsEnding(handle))
       {
        int lineStart=(int)FileTell(handle);
        string order=FileReadString( handle);
        if(FileIsLineEnding(handle))continue;
        string symbol=FileReadString(handle);
        if(FileIsLineEnding(handle))continue;
        string SL=FileReadString(handle);
        if(FileIsLineEnding(handle))continue;
        string TP=FileReadString(handle);
        if(FileIsLineEnding(handle))
          {
            if(StringConcatenate(order,symbol,SL,TP)==
            StringConcatenate(_order,_symbol,_SL,_TP))
             {
              string blankSpace="";
              int lineLen=StringLen(StringConcatenate(order,symbol,SL,TP))+3;
              FileSeek(handle,lineStart,SEEK_SET);
              for(int l=0;l<=lineLen;l++)
                 blankSpace+=" ";
              FileWrite(handle,order,symbol,SL,TP,"Book Profit");
              FileFlush(handle);
             }
          }
       }
    }
 }

相关问题