FileCopy
FileCopy
此函数将原始文件从本地或共享文件夹复制到另一个文件。
bool FileCopy(
const string src_file_name, // Name of a source file
int common_flag, // Location
const string dst_file_name, // Name of the destination file
int mode_flags // Access mode
);参数
- src_file_name
[in] 要复制的文件名。
- common_flag
[in] 标志用于确定文件的位置。如果 common_flag = FILE_COMMON,则文件位于所有客户端终端的共享文件夹 \Terminal\Common\Files 中。否则,文件位于本地文件夹中(例如,common_flag=0)。
- dst_file_name
[in] 结果文件名。
- mode_flags
[in] 访问标志。该参数只能包含 2 个标志:FILE_REWRITE 和/or FILE_COMMON - 其他标志将被忽略。如果文件已存在,且未指定 FILE_REWRITE 标志,则不会重写文件,函数将返回 false。
返回值
如果失败,函数将返回 false。
注意
出于安全原因,MQL4 语言中处理文件的行为受到严格控制。使用 MQL4 进行文件操作的文件不能位于文件沙箱之外。
如果新文件已存在,复制操作将根据 mode_flags 参数中 FILE_REWRITE 标志的存在情况进行执行。
示例:
//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- input parameters
input string InpSrc="source.txt"; // source
input string InpDst="destination.txt"; // copy
input int InpEncodingType=FILE_ANSI; // ANSI=32 or UNICODE=64
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- display the source contents (it must exist)
if(!FileDisplay(InpSrc))
return;
//--- check if the copy file already exists (may not be created)
if(!FileDisplay(InpDst))
{
//--- the copy file does not exist, copying without FILE_REWRITE flag (correct copying)
if(FileCopy(InpSrc,0,InpDst,0))
Print("File is copied!");
else
Print("File is not copied!");
}
else
{
//--- the copy file already exists, try to copy without FILE_REWRITE flag (incorrect copying)
if(FileCopy(InpSrc,0,InpDst,0))
Print("File is copied!");
else
Print("File is not copied!");
//--- InpDst file's contents remains the same
FileDisplay(InpDst);
//--- copy once more with FILE_REWRITE flag (correct copying if the file exists)
if(FileCopy(InpSrc,0,InpDst,FILE_REWRITE))
Print("File is copied!");
else
Print("File is not copied!");
}
//--- receive InpSrc file copy
FileDisplay(InpDst);
}
//+------------------------------------------------------------------+
//| Read the file contents |
//+------------------------------------------------------------------+
bool FileDisplay(const string file_name)
{
//--- reset the error value
ResetLastError();
//--- open the file
int file_handle=FileOpen(file_name,FILE_READ|FILE_TXT|InpEncodingType);
if(file_handle!=INVALID_HANDLE)
{
//--- display the file contents in the loop
Print("+---------------------+");
PrintFormat("File name = %s",file_name);
while(!FileIsEnding(file_handle))
Print(FileReadString(file_handle));
Print("+---------------------+");
//--- close the file
FileClose(file_handle);
return(true);
}
//--- failed to open the file
PrintFormat("%s is not opened, error = %d",file_name,GetLastError());
return(false);
}最后更新于