Read an external file

 

Hi people, I nedd read into an extern file bt it doesn't work

FileHandle=FileOpen("C:\Data\Text.txt",FILE_READ|FILE_WRITE|FILE_TXT,';');

  FileSeek(FileHandle,0, SEEK_SET);

  if(FileHandle!=INVALID_HANDLE){

    while(!FileIsEnding(FileHandle)){

      res=FileReadString(FileHandle);

    }

  }

  FileClose(FileHandle);



can someone help me? Thanks

 

Hello Green Kobra

You cant read outside the the MQL folder with native functions you would need something like this 

#property version   "1.00"
#property strict
#define handle int
#define GENERIC_READ 1<<31
#define GENERIC_WRITE 1<<30
#define GENERIC_EXECUTE 1<<29
#define GENERIC_ALL 1<<28
#define SHARE_NONE 0x00000000
#define SHARE_DELETE 0x00000004
#define SHARE_READ 0x00000001
#define SHARE_WRITE 0x00000002
struct security_attributes
{
int nLength;
int lpSecurityDescriptor;
bool bInheritHandle;
security_attributes(void){nLength=0;lpSecurityDescriptor=NULL;bInheritHandle=false;}
};
security_attributes DEFAULT_SECURITY_ATTRIBUTES;
#define CREATE_NEW 1
#define CREATE_ALWAYS 2
#define OPEN_EXISTING 3
#define OPEN_ALWAYS 4
#define TRUNCATE_EXISTING 5
#define FILE_ATTRIBUTE_ARCHIVE 32
#define FILE_ATTRIBUTE_ENCRYPTED 16384
#define FILE_ATTRIBUTE_HIDDEN 2
#define FILE_ATTRIBUTE_NORMAL 128
#define FILE_ATTRIBUTE_OFFLINE 4096
#define FILE_ATTRIBUTE_READONLY 1
#define FILE_ATTRIBUTE_SYSTEM 4
#define FILE_ATTRIBUTE_TEMPORARY 256
#define FILE_INVALID_FILE_ATTRIBUTES -1
#define FILE_ATTRIBUTE_COMPRESSED 2048
#define FILE_ATTRIBUTE_DEVICE 64
#define FILE_ATTRIBUTE_DIRECTORY 16
#define FILE_ATTRIBUTE_INTEGRITY_STREAM 32768
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 8192
#define FILE_ATTRIBUTE_NO_SCRUB_DATA 131072
#define FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS 4194304
#define FILE_ATTRIBUTE_RECALL_ON_OPEN 262144
#define FILE_ATTRIBUTE_REPARSE_POINT 1024
#define FILE_ATTRIBUTE_SPARSE_FILE 512
#define FILE_ATTRIBUTE_VIRTUAL 65536
#import "kernel32.dll"
handle CreateFileW(string file_name,int access,int share,int intptr,int dwCreationDisposition,int dwFlagsAndAttributes,int hTemplateFile);
bool WriteFile(handle file_handle,string text,int bytes,int &result_bytes,int ptr);
bool ReadFile(handle file_handle,string &receive,int max_bytes_to_read,int &read,int ptr);
bool CreateDirectoryW(string path,int sec);
bool DeleteFileW(string path);
int GetFileAttributesW(string path);
bool CloseHandle(handle h);
#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  //WRITE EXAMPLE 
  handle fop=CreateFileW("E:\\testy2.txt",GENERIC_ALL,SHARE_READ|SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  if(fop==INVALID_HANDLE){Print("NOTCREATED");}
  if(fop!=INVALID_HANDLE)
    {
    Print("CREATED");
    int written=0;
    string txt="WRITE_TEST";
    int bytes_to_write=StringLen(txt)*2;
    bool write=WriteFile(fop,txt,bytes_to_write,written,NULL);
    if(write){Print("Written "+IntegerToString(written));}
    if(!write){Print("Write failed");}
    CloseHandle(fop);
    }
  //WRITE EXAMPLE 
  //READ EXAMPLE 
    fop=CreateFileW("E:\\testy2.txt",GENERIC_READ,SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(fop==INVALID_HANDLE){Print("CANTOPEN");}
    if(fop!=INVALID_HANDLE)
    {
    Print("OPENED");
    int read=0;
    string txt="";
    int max_bytes_to_read=2000;
    bool isread=ReadFile(fop,txt,max_bytes_to_read,read,NULL);
    if(!isread){Print("CANTREAD");}
    if(isread){
    Print("Read "+IntegerToString(read));
    Print("{"+txt+"}");
    }
    CloseHandle(fop);
    }
  //READ EXAMPLE 
  //directory test 
    bool created_dir=CreateDirectoryW("E:\\TestPATH",NULL);
    if(created_dir){Print("CreatedDIR");}
    if(!created_dir){Print("CantCreateDIR");}
    fop=CreateFileW("E:\\writing.txt",GENERIC_ALL,SHARE_READ|SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(fop!=INVALID_HANDLE){Print("Created writing boolean ,deleting");CloseHandle(fop);}
    int exists=GetFileAttributesW("E:\\writing.txt");
    Print("Exists value "+IntegerToString(exists));
    bool del=DeleteFileW("E:\\writing.txt");
    if(!del){Print("Could not delete");}
    if(del){Print("Deleted");}
    exists=GetFileAttributesW("E:\\writing.txt");
    Print("Does not exist value "+IntegerToString(exists));
  return(INIT_SUCCEEDED);
  } 
 

Hallo Lorentzos, very nice code indeed. And something similar was already produced on this forum in the past, always by using the Win32 API commands that you also use.

But... today I just reported a bug, possibly: https://www.mql5.com/en/forum/353944.

Have you ever checked whether on your machine the use of your library is compatible with printf commands?

Thank you if you let me know. Then, if problem does not exist on your machine, this would mean that my PC is scrambled up.

Buggish interaction between winfile_v600.mqh and PrintFormat
Buggish interaction between winfile_v600.mqh and PrintFormat
  • 2020.10.21
  • www.mql5.com
Hallo people, I was testing in mql5 the mql5-slightly-modified winfile_v600.mqh library provided to the community by the Mighty JC ( https://www...
 
thank you very much, now I see how to integrate this code cl my expert advisor
Reason: