FileWriteComplexDouble

从文件指针的当前位置开始,将 complex 参数的值写入二进制文件或 CSV 文件。

uint  FileWriteComplexDouble(
   int      file_handle,      // 文件句柄
   complex  value             // 要写入的值
   );

参量

file_handle

[in]  由 FileOpen() 返回的文件描述符。

value

[in]  complex 类型的值。

返回值

如果成功,函数返回写入的字节数。文件指针移动相同数量的字节。

注意

要获取错误信息,请调用 GetLastError()。

二进制文件中写入 16 字节。

文本文件或 CSV 文件中写入复数的文本表示。

示例 :

//+------------------------------------------------------------------+
//|                                         TestWriteReadComplex.mq5 |
//|                                  Copyright 2026, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| 脚本程序启动函数                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   complex  complex_value=1.012345+0.0001i;
   complexf complexf_value=complexf(0.00678+0.005i);
//--- 写入 CSV 文件
   int  file_handle=FileOpen("complex.csv",FILE_CSV|FILE_WRITE|FILE_ANSI);
   uint written=FileWriteComplexDouble(file_handle,complex_value);
   Print("written ",written," bytes");
   FileWriteString(file_handle,"\t");
   written=FileWriteComplexFloat(file_handle,complexf_value);
   Print("written ",written," bytes");
   FileWriteString(file_handle,"\n");
   FileClose(file_handle);
   complex  complex_value2;
   complexf complexf_value2;
//--- 从 CSV 文件读取
   file_handle=FileOpen("complex.csv",FILE_CSV|FILE_READ|FILE_ANSI);
   complex_value2=FileReadComplexDouble(file_handle);
   complexf_value2=FileReadComplexFloat(file_handle);
   FileClose(file_handle);
   Print("complex_value2=",complex_value2);
   Print("complexf_value2=",complexf_value2);
//--- 写入二进制文件
   file_handle=FileOpen("complex.bin",FILE_BIN|FILE_WRITE);
   written=FileWriteComplexDouble(file_handle,complex_value);
   Print("\nwritten ",written," bytes");
   written=FileWriteComplexFloat(file_handle,complexf_value);
   Print("written ",written," bytes");
   FileClose(file_handle);
   complex  complex_value3;
   complexf complexf_value3;
//--- 从二进制文件读取
   file_handle=FileOpen("complex.bin",FILE_BIN|FILE_READ);
   complex_value3=FileReadComplexDouble(file_handle);
   complexf_value3=FileReadComplexFloat(file_handle);
   FileClose(file_handle);
   Print("complex_value3=",complex_value3);
   Print("complexf_value3=",complexf_value3);
  }

Experts 日志中的输出

   written 17 bytes
   written 27 bytes
   complex_value2=(1.012345,0.0001)
   complexf_value2=(0.0067799999,0.0049999999)
   
   written 16 bytes
   written 8 bytes
   complex_value3=(1.012345,0.0001)
   complexf_value3=(0.0067799999,0.0049999999)

 

另见

复数 (complex, complexf), 实数类型 (double, float), FileWriteDouble, FileWriteComplexFloat