file creation fails

 

Hello,

i try to write a file in the file section. The should be created.

 

string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
string filename=terminal_data_path+"\\MQL5\\Files\\"+"myfile.txt";

int filehandle=FileOpen(filename,FILE_READ|FILE_WRITE|FILE_TXT);
/// filehandle < 0
filehandle=FileOpen(filename,FILE_WRITE|FILE_TXT);
/// filehandle < 0

GetLastError() returns: 5002 -> file not exists

 The folder exists. Can check it with explorer an print out to console.


What is wrong ?

Thank you

 
chinaski:

Hello,

i try to write a file in the file section. The should be created.

 The folder exists. Can check it with explorer an print out to console.

What is wrong ?

Thank you

What is wrong ?. The wrong part is that you read the wrong example. Here's the example from FileOpen

void OnStart()
  {
//--- incorrect file opening method <<== PLEASE READ THIS
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL5\\Files\\"+"fractals.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }
 
//--- correct way of working in the "file sandbox" <<== PLEASE READ THIS
   ResetLastError();
   filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(),PERIOD_CURRENT);
      FileClose(filehandle);
      Print("FileOpen OK");
     }
   else Print("Operation FileOpen failed, error ",GetLastError());
//--- another example with the creation of an enclosed directory in MQL5\Files\
   string subfolder="Research";
   filehandle=FileOpen(subfolder+"\\fractals.txt",FILE_WRITE|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(),PERIOD_CURRENT);
      FileClose(filehandle);
      Print("The file most be created in the folder "+terminal_data_path+"\\"+subfolder);
     }
   else Print("File open failed, error ",GetLastError());
  }

 

 
phi.nuts:

What is wrong ?. The wrong part is that you read the wrong example. Here's the example from FileOpen

 

Hello Phi,

This works. Thank you. No path is the solution (one of the mql miracles: even path created in my sample was correctly).

 

New problem, file reading:

 

My file has 2 ANSI lines:

 

ABCD

EFGH

 

int filehandle=FileOpen("myexistingfile",FILE_READ|FILE_TXT|FILE_ANSI);
if(filehandle != INVALID_HANDLE)
        {
            string  content=FileReadString(filehandle,-1);
        }

 content is only ABCD.

 (content was written (ANSI)  to file with "\r\n" to separate rows).

How to get the whole content, please ?

Thank you 

 
chinaski:

New problem, file reading:

 

My file has 2 ANSI lines:

 

ABCD

EFGH

 

 content is only ABCD.

 (content was written (ANSI)  to file with "\r\n" to separate rows).

How to get the whole content, please ?

Thank you 

So you have more than 1 line. How about also more than 1 column ? like this :

ABCD, EXGH, IJKL

MNOP, QRST, ,

UVWX, YZ12, 3456, 7890  

Stay tune, I'll be back ! ;D 

... or use mql5 search 

 
phi nuts #:

What is wrong ?. The wrong part is that you read the wrong example. Here's the example from FileOpen

 

this does not work. After bit of correction it spams FileOpen OK but still no file.

Reason: