FileWriteString
FileWriteString
该函数将字符串类型的参数值写入BIN、CSV或TXT文件,从文件指针的当前位置开始。当写入CSV或TXT文件时:如果字符串中包含“\n”(LF)而没有之前的“\r”(CR),则在“\n”之前会添加缺失的“\r”。
uint FileWriteString(
int file_handle, // File handle
const string text_string, // string to write
int length=0 // number of symbols
);参数
- file_handle
[in] 由FileOpen()返回的文件描述符。
- text_string
[in] 字符串。
- length=0
[in] 想要写入的字符数。此选项用于将字符串写入BIN文件。如果未指定大小,则将整个字符串不包括尾部0字符写入。如果指定的大小小于字符串长度,则只写入字符串中不包括尾部0的部分。如果指定的大小大于字符串长度,则使用适当数量的零填充字符串。对于CSV和TXT类型的文件,此参数将被忽略,字符串将完全写入。
返回值
如果成功,函数返回写入的字节数;如果出错,则返回0。要获取关于错误的信息,请调用GetLastError()函数。文件指针将移动相同数量的字节。
注意
请注意,当使用FILE_UNICODE标志(或没有FILE_ANSI标志)打开的文件进行写入时,写入的字节数将是写入的字符串字符数的两倍。当使用FILE_ANSI标志打开的文件进行记录时,写入的字节数将与写入的字符串字符数一致。
示例:
//+------------------------------------------------------------------+
//| Demo_FileWriteString.mq4 |
//| Copyright 2014, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for receiving data from the terminal
input string InpSymbolName="EURUSD"; // Currency pair
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_H1; // Time frame
input int InpMAPeriod=14; // MA period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Price type
//--- parameters for writing data to the file
input string InpFileName="RSI.csv"; // File name
input string InpDirectoryName="Data"; // Folder name
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
double rsi_buff[]; // array of indicator values
datetime date_buff[]; // array of the indicator dates
int rsi_size=0; // size of the indicator arrays
//--- set indexing as timeseries
ArraySetAsSeries(rsi_buff,true);
ArraySetAsSeries(date_buff,true);
//--- reset last error code
ResetLastError();
//--- copying the time from last 1000 bars
int copied=CopyTime(NULL,0,0,1000,date_buff);
if(copied<=0)
{
PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
return;
}
//--- prepare rsi_buff array
ArrayResize(rsi_buff,copied);
//--- copy the values of RSI indicator
for(int i=0;i<copied;i++)
{
rsi_buff[i]=iRSI(InpSymbolName,InpSymbolPeriod,InpMAPeriod,InpAppliedPrice,i);
}
//--- get size
rsi_size=ArraySize(rsi_buff);
//--- open the file for writing the indicator values (if the file is absent, it will be created automatically)
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for writing",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- prepare additional variables
string str="";
bool is_formed=false;
//--- write dates of forming overbought and oversold areas
for(int i=0;i<rsi_size;i++)
{
//--- check the indicator values
if(rsi_buff[i]>=70 || rsi_buff[i]<=30)
{
//--- if the value is the first one in this area
if(!is_formed)
{
//--- add the value and the date
str=(string)rsi_buff[i]+"\t"+(string)date_buff[i];
is_formed=true;
}
else
str+="\t"+(string)rsi_buff[i]+"\t"+(string)date_buff[i];
//--- move to the next loop iteration
continue;
}
//--- check the flag
if(is_formed)
{
//--- the string is formed, write it to the file
FileWriteString(file_handle,str+"\r\n");
is_formed=false;
}
}
//--- close the file
FileClose(file_handle);
PrintFormat("Data is written, %s file is closed",InpFileName);
}
else
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
}另请参阅
最后更新于