Working with files. Part II

[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]

This article continues the precedent article Working with files . Part I and it’s intended to be it’s practical side.

The article shows in one code the use of the functions shown in Part I.

Before this, a few notes.
MetaQuotes changed the prototype of the FileOpen() function.

1
2
3
4
5
6
int  FileOpen(
   string  file_name,           // File name
   int     open_flags,          // Combination of flags
   short   delimiter='t'       // Delimiter
   uint    codepage=CP_ACP      // Code page
   );

What was added is the codepage, that serves at the conversion of string variables. Default is CP_ACP (Ansi Code Page).

To be known when using FileOpen(): if FILE_ANSI is not used, FILE_UNICODE is presumed; even the end of the line is awaited as unicode (CRLF spanning 4 bytes instead of 2).

Regarding the FileIsLineEnding() function, whose application seemed unclear in the previous article, there is a small glitch to be known: indeed, function works only on FILE_TXT files, and it answers true if the end of the line is reached by using FileReadString(). Not by seeking. However, remember that most text files are ANSI. Don’t forget to specify FILE_ANSI. Otherwise all readings will be abnormal, including the fact that FileIsLineEnding() will not report correctly!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//+------------------------------------------------------------------+
//|                                                WorkWithFiles.mq5 |
//|                                         Copyright Baltatu Bogdan |
//|                                           http:\mqlmagazine.com |
//+------------------------------------------------------------------+
#property copyright "Baltatu Bogdan"
#property link      "http:\mqlmagazine.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
 
void OnStart()
  {
   int file_handle,size=-1,i;
   bool res;
   string svalue,filename;
   long search;
//FileOpen
   file_handle=FileOpen("test.txt",FILE_WRITE|FILE_TXT);
   if(file_handle==-1) Print("FileOpen error.ErrorNo:",GetLastError());
   else Print("FileOpen success!");
//FileIsExist
   res=FileIsExist("test.txt");
   if(res)Print("File exist!!");
   else Print("File not found!!");
//FileWrite
   svalue="abcdefrnghijkl";
   size=StringLen(svalue);
   FileWriteString(file_handle,svalue,size);
//FileFlush
   FileFlush(file_handle);
//FileClose
   FileClose(file_handle);
   file_handle=FileOpen("test.txt",FILE_TXT|FILE_READ);
//FileTell
   FileTell(file_handle);
   Print("Cursor position:",FileTell(file_handle));
//FileSeek
   FileSeek(file_handle,0,SEEK_SET);
//FileClose
   FileClose(file_handle);
//FileCopy
   res=FileCopy("test.txt",0,"test1.txt",FILE_REWRITE);
   if(res) Print("FileCopy Succes");
   else Print("FileCopy Faild.ErrorNo:",GetLastError());
//FileMove
   res=FileMove("test.txt",0,"move_test.txt",FILE_REWRITE);
   if(res) Print("FileMove Succes!");
   else Print("FileMove Faild!ErrorNo:",GetLastError());
//FileDelete
   search=FileFindFirst("*.*",filename);
   if(search!=INVALID_HANDLE)
     {
      Print("FileFindFirst returned",filename);
      while(FileFindNext(search,filename))
        {
         i++;
         Print(i,":",filename);
        }
      FileFindClose(filename);
     }
   else
      Print("Files not found!!!");
 
   FileDelete("test.txt");
  }
//+------------------------------------------------------------------+