Read File Function build 600

 

Hello,

i have a ReadFile Function which does not work any more in the new build 600, can somebody help me or is there another new example for ReadFile function?

Here is the Code from my Current Read File function:

#property copyright   "MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description ""


#define OF_READ               0
#define OF_WRITE              1
#define OF_READWRITE          2
#define OF_SHARE_COMPAT       3
#define OF_SHARE_DENY_NONE    4
#define OF_SHARE_DENY_READ    5
#define OF_SHARE_DENY_WRITE   6
#define OF_SHARE_EXCLUSIVE    7
 
 
#import "kernel32.dll"
   int _lopen  (string path, int of);
   int _lcreat (string path, int attrib);
   int _llseek (int handle, int offset, int origin);
   int _lread  (int handle, string buffer, int bytes);
   int _lwrite (int handle, string buffer, int bytes);
   int _lclose (int handle);
#import



//+------------------------------------------------------------------+
//| OnTick function                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
  string response="initial";
  response = ReadFile("C:\Program Files\MetaTrader4\MQL4\Files\a.txt");
  Comment(response);
//---
  }
//+------------------------------------------------------------------+


//+------------------------------------------------------------------+
//| Diese Funktion lest den Text aus einer Datei und speichert       |
//| den gelesenen Text in eine Variable.                             |
//+------------------------------------------------------------------+

string ReadFile(string path) 
{
//----
    int handle=_lopen (path,OF_READ);           
    
    if(handle<0) 
      {
        Print("Error opening file ",path); 
        return ("");
      }
   
    int result=_llseek (handle,0,0);      
    
    if(result<0) 
      {
        Print("Error placing the pointer" ); 
        return ("");
      }
    
    string buffer="";
    string char1="x";
    int count=0;
    result = _lread (handle,char1,1);
    
    while(result>0) 
       {
        buffer = buffer+char1;
        char1 = "x";
        count++;
        result = _lread (handle,char1,1);
       }
    
    result = _lclose (handle);              
    
    if(result<0)  
      Print("Error closing file ",path);
    
    return (buffer);
//----
}
 
You can't use functions such as _lopen() in v600 (or, at least, not easily). You need to use newer Win32 functions instead. See https://www.mql5.com/en/forum/147939 for a related question
 
gchrmt4:
You can't use functions such as _lopen() in v600 (or, at least, not easily). You need to use newer Win32 functions instead. See https://www.mql5.com/en/forum/147939 for a related question

Can you give me a example how i can read the file into a string with the new Win32 functions? I can open also a job if you want and pay for your help. I need just the functions to read the file into a string and give this string back.
 

Replacement ReadFile() function as follows. But you don't need this to read files from MT4's own directory (MQL4\Files); you can use the built-in file functions for that.

Note that this is unreliable:

ReadFile("C:\Program Files\MetaTrader4\MQL4\Files\a.txt");

You should use this instead, for the reasons set out in https://www.mql5.com/en/forum/147939

ReadFile("C:\\Program Files\\MetaTrader4\\MQL4\\Files\\a.txt");

#import "kernel32.dll"
   int CreateFileW(string, uint, int, int, int, int, int);
   int GetFileSize(int, int);
   int ReadFile(int, uchar&[], int, int&[], int);
   int CloseHandle(int);
#import

string ReadFile(string Filename)
{
   string strFileContents = "";
   int h = CreateFileW(Filename, 0x80000000 /*GENERIC_READ*/, 3 /*SHARE READ|WRITE*/, 0, 3 /*OPEN_EXISTING*/, 0, 0);
   if (h == -1) {
      // Open failed   
   } else {
      int sz = GetFileSize(h, 0);
      if (sz > 0) {
         uchar buffer[];
         ArrayResize(buffer, sz);
         int read[1];
         ReadFile(h, buffer, sz, read, 0);
         if (read[0] == sz) {
            strFileContents = CharArrayToString(buffer, 0, read[0]);
         } else {
            // Read failed
         }
      } else {
         // Empty file
      }
      CloseHandle(h);
   }
   return strFileContents;
}
 
Thank you very much.
 
How to write file with WriteFile (kernel32.dll). Can anybody help me ?
Reason: