MQL4 uses the same mechanism as many other languages such as C and Javascript where you can put special characters in a string constant by prefacing them with \
For example, "Column1\tColumn2" means "Column1 <tab> Column2". Similarly, "Column1\nColumn2" means "Column1 <new-line> Column2"
Because the \ character has a special meaning, you cannot just use \ in a string constant to mean "backslash". You have to use \\
\\ gets replaced with \ in the same way that \t gets replaced with <tab> and \n gets replaced with <new-line>
[in] File property ID. The value can be one of the values of the ENUM_FILE_PROPERTY_INTEGER enumeration. If the second variant of the function is used, you can receive only the values of the following properties: [in] Points to the file location. If the parameter is false, terminal data folder is viewed. Otherwise it is assumed that the file...
please i want to read and write to external files in mql4
i came across this documentation https://www.mql5.com/en/articles/1540 but it didnt work for me
firstly i downloaded the kernel32 winAPI and placed it in mt4 libraries file and implemented he documentation code
but it didnt work.
please help
#define GENERIC_READ 0x80000000#define GENERIC_WRITE 0x40000000#define FILE_SHARE_READ0x00000001#define FILE_SHARE_WRITE0x00000002#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_END2#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, int BufferPtr, 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);
bool DeleteFileA(string Filename);
int MulDiv(string X, int N1,int N2);
// Used for temporary conversion of an array into a block of memory, which// can then be passed as an integer to ReadFileint LocalAlloc(int Flags, int Bytes);
int RtlMoveMemory(int DestPtr, double & Array[], int Length);
int LocalFree(int lMem);
// Used for converting the address of an array to an integer int GlobalLock(double & Array[]);
bool GlobalUnlock(int hMem);
#import
int OpenNewFileForWriting(string FileName, bool ShareForReading = false)
{
int ShareMode = 0;
if (ShareForReading) ShareMode = FILE_SHARE_READ;
return CreateFileW(FileName, GENERIC_WRITE /*GENERIC_READ*/, 3/*SHARE READ|WRITE*/, 0, CREATE_ALWAYS, 0, 0);
// return (CreateFileA(FileName, GENERIC_WRITE, ShareMode, 0, CREATE_ALWAYS, 0, 0));
}
bool IsValidFileHandle(int FileHandle)
{
return (FileHandle != INVALID_HANDLE_VALUE);
}
void CloseFile(int FileHandle)
{
CloseHandle(FileHandle);
}
bool WriteToFile(int FileHandle, string DataToWrite)
{
// Receives the number of bytes written to the file. Note that MQL can only pass // arrays as by-reference parameters to DLLsint BytesWritten[1] = {0};
// Get the length of the string int szData = StringLen(DataToWrite);
Print(szData);
Print(DataToWrite);
// Do the write
WriteFile(FileHandle, MulDiv(DataToWrite,1,1), szData, BytesWritten, 0);
Print("BytesWritten = " + BytesWritten[0]);
// Return true if the number of bytes written matches the expected number return (BytesWritten[0] == szData);
}
int start()
{
string strTestFilename = "C:\\test2.txt";
Print("About to write some example data to " + strTestFilename + "...");
// Write some CRLF-terminated lines to the test file int fWrite = OpenNewFileForWriting(strTestFilename, true);
if (!IsValidFileHandle(fWrite))
{
Print("Unable to open " + strTestFilename + " for writing");
} else {
if((WriteToFile(fWrite, "Test12345678"))==TRUE) {
Print("write ok");
} else { Print("write not ok"); }
}
}
MQL4 is designed in such a way that even incorrectly written programs are unable to mistakenly delete data from the hard disk. The functions used for file reading and writing operations can work in the following directories only (quote): If you still need to work outside the directories (defined for safety reasons), you can call the functions...
See https://docs.mql4.com/basis/types/string
MQL4 uses the same mechanism as many other languages such as C and Javascript where you can put special characters in a string constant by prefacing them with \
For example, "Column1\tColumn2" means "Column1 <tab> Column2". Similarly, "Column1\nColumn2" means "Column1 <new-line> Column2"
Because the \ character has a special meaning, you cannot just use \ in a string constant to mean "backslash". You have to use \\
\\ gets replaced with \ in the same way that \t gets replaced with <tab> and \n gets replaced with <new-line>
Therefore, if you do the following:
...you should see the output "c:\windows\win.ini"
Oh, yes, undestand it.
Soo, good work, it will not be until after the BS n or t.
See https://www.mql5.com/en/forum/149304
Alain Verleyen
Seeing as someone has dragged up this old thread....
There is now an MQL4 method to get the file modification time
https://docs.mql4.com/files/filegetinteger
hello gurus here please i am stuck in my project.
please i want to read and write to external files in mql4
i came across this documentation https://www.mql5.com/en/articles/1540 but it didnt work for me
firstly i downloaded the kernel32 winAPI and placed it in mt4 libraries file and implemented he documentation code
but it didnt work.
please help