MT5使 kernel32.dll 把字符串写入到csv 文件中。MQL5写入外部文件方法。
下面是MQL5的源码,MQL4也可以用的。
//+------------------------------------------------------------------+
//| 写入文件.mq5 |
//| Copyright 2022, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
#define GENERIC_READ -2147483648
#define GENERIC_WRITE 1073741824
//#define FILE_SHARE_READ 1
//#define FILE_SHARE_WRITE 2
#define CREATE_NEW 1
#define CREATE_ALWAYS 2
#define OPEN_ALWAYS 4
#define OPEN_EXISTING 3
#define TRUNCATE_EXISTING 5
#define FILE_BEGIN 0
#define FILE_CURRENT 1
#define FILE_END 2
#define INVALID_HANDLE_VALUE -1
#import "kernel32.dll"
int CreateFileW(string Filename, int AccessMode, int ShareMode, int PassAsZero, int CreationMode, int FlagsAndAttributes, int AlsoPassAsZero);
//int ReadFile(int FileHandle, int BufferPtr, int BufferLength, int & BytesRead[], int PassAsZero);
int WriteFile(int FileHandle, const uchar &buffer[], int BufferLength, int & BytesWritten, int PassAsZero);
int SetFilePointer(int FileHandle, int Distance, int PassAsZero, int FromPosition);
//int GetFileSize(int FileHandle, int PassAsZero);
int CloseHandle(int FileHandle);
#import
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
int fileHandle = CreateFileW("c:\\test.csv", GENERIC_READ|GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if (fileHandle == INVALID_HANDLE_VALUE)
{
Print("打开文件失败!#",GetLastError());
return ;
}
SetFilePointer(fileHandle, 0, 0, FILE_END);
string str="Hello World!";
uchar buffer[];
StringToCharArray(str,buffer);
int bytesWritten = 0;
WriteFile(fileHandle, buffer, ArraySize(buffer), bytesWritten, 0);
CloseHandle(fileHandle);
}
//+------------------------------------------------------------------+
