How to Read and write data from out side the metatrader folder

 

Hi everyone,Can you please help me hw to read and write file from external folders can you show me some small code so that i understand ??

Many Thanks

 

WINAPI

search the forum there are plenty examples

 
qjol:

WINAPI

search the forum there are plenty examples


Hi thanks for your reply but i want to write the data in csv formt and all the examples that i see is for text format can you please help hw to get that ???
 
ankityadav:

Hi thanks for your reply but i want to write the data in csv formt and all the examples that i see is for text format can you please help hw to get that ???

Make it works first. Then you will know what to do next.
 
deysmacro:

Make it works first. Then you will know what to do next.

Thanks for your reply but it is not working when i m trying to read the file from different folder and write the file
//+------------------------------------------------------------------+
//|                                              File Read Write.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

// êîíñòàíòû äëÿ ôóíêöèè _lopen
#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

// _lopen  : Îòêpûâàåò óêàçàííûé ôàéë. Âîçâpàùàåò: îïèñàòåëü ôàéëà.
// _lcreat : Ñîçäàåò óêàçàííûé ôàéë.   Âîçâpàùàåò: îïèñàòåëü ôàéëà.
// _llseek : Óñòàíàâëèâàåò óêàçàòåëü â îòêpûòîì ôàéëå. Âîçâpàùàåò: 
// íîâîå ñìåùåíèå óêàçàòåëÿ.
// _lread  : Ñ÷èòûâàåò èç îòêpûòîãî ôàéëà óêàçàííîå ÷èñëî áàéò. 
// Âîçâpàùàåò: ÷èñëî ñ÷èòàííûõ áàéò; 0 - åñëè êîíåö ôàéëà.
// _lwrite : Çàïèñûâàåò äàííûå èç áóôåpà â óêàçàííûé ôàéë. Âîçâpàùàåò: 
// ÷èñëî çàïèñàííûõ áàéò.
// _lclose : Çàêpûâàåò óêàçàííûé ôàéë. Âîçâpàùàåò: 0.
//  ñëó÷àå íåóñïåøíîãî çàâåpøåíèÿ âñå ôóíêöèè âîçâðàùàþò çíà÷åíèå 
// HFILE_ERROR=-1.
 
// path   : Ñòpîêà, îïpåäåëÿþùàÿ ïóòü è èìÿ ôàéëà.
// of     : Ñïîñîá îòêðûòèÿ.
// attrib : 0 - ÷òåíèå èëè çàïèñü; 1 - òîëüêî ÷òåíèå; 2 - íåâèäèìûé èëè 
// 3 - ñèñòåìíûé.
// handle : Ôàéëîâûé îïèñàòåëü.
// offset : ×èñëî áàéò, íà êîòîpîå ïåpåìåùàåòñÿ óêàçàòåëü.
// origin : Óêàçûâàåò íà÷àëüíóþ òî÷êó è íàïpàâëåíèå ïåpåìåùåíèÿ: 0 - 
// âïåpåä îò íà÷àëà; 1 - ñ òåêóùåé ïîçèöèè; 2 - íàçàä îò êîíöà ôàéëà.
// buffer : Ïpèíèìàþùèé/çàïèñûâàåìûé áóôåp.
// bytes  : ×èñëî ñ÷èòûâàåìûõ áàéò.
 
// Ñïîñîáû îòêðûòèÿ (ïàðàìåòð of):
// int OF_READ            =0; // Îòêðûòü ôàéë òîëüêî äëÿ ÷òåíèÿ
// int OF_WRITE           =1; // Îòêðûòü ôàéë òîëüêî äëÿ çàïèñè
// int OF_READWRITE       =2; // Îòêðûòü ôàéë â ðåæèìå çàïèñü/÷òåíèå
// int OF_SHARE_COMPAT    =3; // Îòêðûâàåò ôàéë â ðåæèìå îáùåãî 
// ñîâìåñòíîãî äîñòóïà. Â ýòîì ðåæèìå ëþáîé ïðîöåññ ìîæåò îòêðûòü äàííûé 
// ôàéë ëþáîå êîëè÷åñòâî ðàç. Ïðè ïîïûòêå îòêðûòü ýòîò ôàéë â ëþáîì äðóãîì
// ðåæèìå, ôóíêöèÿ âîçâðàùàåò HFILE_ERROR.
// int OF_SHARE_DENY_NONE =4; // Îòêðûâàåò ôàéë â ðåæèìå îáùåãî äîñòóïà 
// áåç çàïðåòà íà ÷òåíèå/çàïèñü äðóãèì ïðîöåññàì. Ïðè ïîïûòêå îòêðûòèÿ 
// äàííîãî ôàéëà â ðåæèìå OF_SHARE_COMPAT, ôóíêöèÿ âîçâðàùàåò HFILE_ERROR.
// int OF_SHARE_DENY_READ =5; // Îòêðûâàåò ôàéë â ðåæèìå îáùåãî äîñòóïà ñ 
// çàïðåòîì íà ÷òåíèå äðóãèì ïðîöåññàì. Ïðè ïîïûòêå îòêðûòèÿ äàííîãî ôàéëà 
// ñ ôëàãàìè OF_SHARE_COMPAT è/èëè OF_READ èëè OF_READWRITE, ôóíêöèÿ 
// âîçâðàùàåò HFILE_ERROR.
// int OF_SHARE_DENY_WRITE=6; // Òîæå ñàìîå, òîëüêî ñ çàïðåòîì íà çàïèñü.
// int OF_SHARE_EXCLUSIVE =7; // Çàïðåò òåêóùåìó è äðóãèì ïðîöåññàì íà 
// äîñòóï ê ýòîìó ôàéëó â ðåæèìàõ ÷òåíèÿ/çàïèñè. Ôàéë â ýòîì ðåæèìå ìîæíî 
// îòêðûòü òîëüêî îäèí ðàç (òåêóùèì ïðîöåññîì). Âñå îñòàëüíûå ïîïûòêè 
// îòêðûòèÿ ôàéëà áóäóò ïðîâàëåíû.
//+------------------------------------------------------------------+
//|   ïðî÷èòàòü ôàéë è âåðíóòü ñòðîêó ñ ñîäåðæèìûì                   |
//+------------------------------------------------------------------+

int start()
  {
//----
    string buffer=ReadFile("C:\Documents and Settings\Administrator\My Documents\Downloads\test.txt");
    int count=StringLen(buffer);
    Print("Ïðî÷èòàíî áàéò:",count);
    WriteFile("C:\Documents and Settings\Administrator\My Documents\Downloads\test2.txt",buffer);   
//----
   return(0);
  }
  
  string ReadFile (string path) 
  {
    int handle=_lopen (path,OF_READ);           
    if(handle<0) 
      {
        Print("Îøèáêà îòêðûòèÿ ôàéëà ",path); 
        return ("");
      }
    int result=_llseek (handle,0,0);      
    if(result<0) 
      {
        Print("Îøèáêà óñòàíîâêè óêàçàòåëÿ" ); 
        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("Îøèáêà çàêðûòèÿ ôàéëà ",path);
    return (buffer);
  }
 
//+------------------------------------------------------------------+
//|  çàïèñàòü ñîäåðæèìîå áóôåðà ïî óêàçàííîìó ïóòè                   |
//+------------------------------------------------------------------+
void WriteFile (string path, string buffer) 
  {
    int count=StringLen (buffer); 
    int result;
    int handle=_lopen (path,OF_WRITE);
    if(handle<0) 
      {
        handle=_lcreat (path,0);
        if(handle<0) 
          {
            Print ("Îøèáêà ñîçäàíèÿ ôàéëà ",path);
            return;
          }
        result=_lclose (handle);
     }
    handle=_lopen (path,OF_WRITE);               
    if(handle<0) 
      {
        Print("Îøèáêà îòêðûòèÿ ôàéëà ",path); 
        return;
      }
    result=_llseek (handle,0,0);          
    if(result<0) 
      {
        Print("Îøèáêà óñòàíîâêè óêàçàòåëÿ"); 
        return;
      }
    result=_lwrite (handle,buffer,count); 
    if(result<0)  
        Print("Îøèáêà çàïèñè â ôàéë ",path," ",count," áàéò");
    result=_lclose (handle);              
    if(result<0)  
        Print("Îøèáêà çàêðûòèÿ ôàéëà ",path);
  }
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
 
C:\Documents and Settings\Administrator\My Documents\Downloads\test.txt
Just change directory to C:\ and make sure test.txt is created by you.
 
deysmacro:
Just change directory to C:\ and make sure test.txt is created by you.

I don't understand i put it in to download folder and i have created both files but not working and i don't understand what your saying??It is neccessary to put the file in c:folder ???
 
 

Thanks but i don't understand before i m using kernel and now Winfile ????
 
ankityadav:

Thanks but i don't understand before i m using kernel and now Winfile ????

Have looked at it? search for kernel32.
 
gooly:

Have looked at it? search for kernel32.

Thanks i already search for this but can you let me know a sample of flie read write operaton in csv file in any folder ??