FileOpen and FileWrite

 

Hi,

I got a problem using FileOpen and FileWrite. The functions FileOpen and FileWrite seem to work (returns positive return val) but no file is created... see code below

I'll be gratefull for help.

code:

int outFile = FileOpen("testFileOpen.csv",FILE_CSV|FILE_WRITE,',');
if (outFile<=0){
Alert("File was not opened ");
}
else{
Alert("File was opened ");
int charsWritten = FileWrite(outFile,"test file write");
if (charsWritten <=0)
Alert("Cant write to file");
else
Alert(charsWritten," Chars written");

FileClose(outFile);
}

 

What did Alert say?

What OS? If Vista or W7, look for files in virtual directories.

 

are you looking in correct folders?


tester\files

expert\files

 

The alerts said:

"File was open"

"16 Chars written"

So it seems that both the FileOpen and the FileWrite succeded.

I searched for the created file in the folder C:\Program Files\Fxglobe ECN MetaTrader 4.0 (I used a file search which includes sub-directories)

I also looked manually in the folders:

C:\Program Files\Fxglobe ECN MetaTrader 4.0\tester\files

C:\Program Files\Fxglobe ECN MetaTrader 4.0\experts\files

* I use a demo account

 
sip:

The alerts said:

"File was open"

"16 Chars written"

So it seems that both the FileOpen and the FileWrite succeded.

I searched for the created file in the folder C:\Program Files\Fxglobe ECN MetaTrader 4.0 (I used a file search which includes sub-directories)

I also looked manually in the folders:

C:\Program Files\Fxglobe ECN MetaTrader 4.0\tester\files

C:\Program Files\Fxglobe ECN MetaTrader 4.0\experts\files

* I use a demo account

Hello,


You can inspire it works only in writing.
Compile it, run it from the browser MT4 which was summarized,
look in the program file FXCM> Experts> Files ...

Cordially

//+------------------------------------------------------------------+
//| essai .mq4 |
//| Copyright © 2010, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int Handle, nbre=0;//, sens=2, ticket=3333;
string File_Name="trading.txt", sens=2, ticket=3333;
//-----Lecture-------------------------------------------------------+
Handle=FileOpen(File_Name,FILE_CSV|FILE_READ,";");// File opening
if(Handle<0) // File opening fails
{
if(GetLastError()==4103) // If the file does not exist,..
Alert("No file named ",File_Name);//.. inform trader
else // If any other error occurs..
Alert("Error while opening file ",File_Name);//..this message
PlaySound("Bzrrr.wav"); // Sound accompaniment
return; // Exit start()
}
//--------------------------------------------------------------- 4 --
while(FileIsEnding(Handle)==false) // While the file pointer..je n'ai qu'un
{ // ..is not at the end of the file
//--------------------------------------------------------- 5 --
sens =FileReadString(Handle);// Position
ticket=FileReadString(Handle);// No d'ordre
if(FileIsEnding(Handle)==true) // File pointer is at the end
break; // Exit reading
//---Traitement------------------------------------------------------ 7 --

}
//--------------------------------------------------------------- 8 --
FileClose( Handle ); // Close file
PlaySound("bulk.wav"); // Sound accompaniment
WindowRedraw(); // Redraw object
// return; // Exit start()

//--------------------------------------------------------------- 9 --
//int sensw, ticketw=33;
// if(sens=="7") sens=1;
sens=1;
ticket=22222;
//----Ecriture-------------------------------------------------------+
Handle=FileOpen(File_Name,FILE_WRITE,";");// File opening
if(Handle<0) // File opening fails
{
if(GetLastError()==4103) // If the file does not exist,..
Alert("No file named ",File_Name);//.. inform trader
else // If any other error occurs..
Alert("Error while opening file ",File_Name);//..this message
PlaySound("Bzrrr.wav"); // Sound accompaniment
return; // Exit start()
}
//--------------------------------------------------------------- 4 --
nbre=FileWrite(Handle,sens,ticket);//Writing to the file
if(nbre < 0) // If failed
{
Alert("Error writing to the file",GetLastError());// Message
PlaySound("Bzrrr.wav"); // Sound accompaniment
FileClose( Handle ); // File closing
return; // Exit start()
}
//--------------------------------------------------------------- 7 --
FileClose( Handle ); // File closing
Alert("The ",File_Name," file created.");// Message
PlaySound("Bulk.wav"); // Sound accompaniment
return; // Exit start()
}
//--------------------------------------------------------------- 8 --

 

Thanks for the code Cordially. When I use it as an EA and I get alerts saying: "No file named trading.txt"

It seems that the file is not created. The directories:

C:\Program Files\Fxglobe ECN MetaTrader 4.0\experts\files

and

C:\Program Files\Fxglobe ECN MetaTrader 4.0\tester\files

are still empty.

 

I found the problem. Windows 7 prevent writing to some directories (such as program files or system directories) for non administrator users (though my user has administrator privilages).

I needed to right click on the program's exe file and choose "Run as administrator". That solved the issue! :)

Thanks for trying for help!

 
How to change the Default saving directory to c:\csv ?.
 
Don't install in \program files* on Vista/Win7. I've found that "Run as administrator" doesn't solve all issues.
 
d.saravana21:
How to change the Default saving directory to c:\csv ?.
You can't.
 
sip:

I found the problem. Windows 7 prevent writing to some directories (such as program files or system directories) for non administrator users (though my user has administrator privilages).

I needed to right click on the program's exe file and choose "Run as administrator". That solved the issue! :)

Thanks for trying for help!


Hi Sip,

 

Can you tell me the name of the  exe file that needs to be granted "Run as administrator"?

 Thanks,

Lars. 

Reason: