File Read Write

Download script
Author: Copyright � 2008, MetaQuotes Software Corp.
File Read Write

This script serves as a foundational utility for managing external text files within the MetaTrader environment. It provides the necessary logic to bridge the gap between the trading platform and your computer's hard drive, specifically allowing the software to read information from one file and save it into another.

Core Functionality

The logic is divided into two primary actions: Reading and Writing.

1. Data Retrieval (Reading) The script contains a specific set of instructions to locate a text file on your device (for example, a file named Text.txt). It performs the following steps:

  • Accessing the File: It attempts to open the specified file in "Read-Only" mode. If the file cannot be found or accessed, it notifies the system with an error message.
  • Positioning: Once opened, it ensures the "cursor" is at the very beginning of the document to make sure no data is skipped.
  • Data Extraction: It reads the file character by character, assembling the entire content into a single piece of data (a string) that the program can hold in its memory.
  • Cleanup: After all the information is gathered, it properly closes the file to ensure it isn't "locked" for other uses.

2. Data Storage (Writing) The script also possesses the logic to create or update files. It takes the data held in the program's memory and saves it to a new destination (for example, Text2.txt):

  • File Creation: It first checks if the target file already exists. If it doesn't find one, the script is designed to automatically create a brand-new file.
  • Data Overwriting: It opens the destination file in "Write" mode and places the cursor at the beginning. This ensures that whatever was previously in the file is replaced by the new information.
  • Execution: It pours the entire contents of the memory buffer into the file and then closes the connection safely.

Automated Workflow

When this script is executed, it follows a linear, automated "pipeline":

  1. It reaches out to your hard drive to grab everything inside C:\Text.txt.
  2. It calculates exactly how much information it managed to collect and reports that number back to the system logs.
  3. It immediately takes that exact same content and copies it into a new file at C:\Text2.txt.

In summary, this script acts as a reliable "Copy-Paste" engine, moving data from one specific location on your computer to another while handling all the technical heavy lifting of opening, navigating, creating, and closing files automatically.

8 Views
0 Downloads
0 Favorites
File Read Write
//+------------------------------------------------------------------+
//|                                              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; // Çàïðåò òåêóùåìó è äðóãèì ïðîöåññàì íà 
// äîñòóï ê ýòîìó ôàéëó â ðåæèìàõ ÷òåíèÿ/çàïèñè. Ôàéë â ýòîì ðåæèìå ìîæíî 
// îòêðûòü òîëüêî îäèí ðàç (òåêóùèì ïðîöåññîì). Âñå îñòàëüíûå ïîïûòêè 
// îòêðûòèÿ ôàéëà áóäóò ïðîâàëåíû.
//+------------------------------------------------------------------+
//|   ïðî÷èòàòü ôàéë è âåðíóòü ñòðîêó ñ ñîäåðæèìûì                   |
//+------------------------------------------------------------------+
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                                    |
//+------------------------------------------------------------------+
int start()
  {
//----
    string buffer=ReadFile("C:\\Text.txt");
    int count=StringLen(buffer);
    Print("Ïðî÷èòàíî áàéò:",count);
    WriteFile("C:\\Text2.txt",buffer);   
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments