MQL4 error 5004 and 5002 when try to read file - mql4

I'm coding in MQL4 to read a file. When I just define the filename and put the file in specified place it shown error 5004. But when I define the path it shown 5002. I've been to MetaTrader forum and found this (https://www.mql5.com/en/forum/7049) thread. But still not solve. Did I miss something here?
string filename = TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files\\output.txt";
Print(filename);
ResetLastError();
int file_handle=FileOpen("out.txt", FILE_READ|FILE_TXT);
//int file_handle=FileOpen(filename, FILE_TXT|FILE_READ);
//Print(file_handle);
string up, down, sideway;
up = down = sideway = 0;
if (file_handle!=INVALID_HANDLE){
Print("read");
up=FileReadString(file_handle);
down = FileReadString(file_handle);
sideway = FileReadString(file_handle);
} else{
Print("file open error: ", GetLastError());
} FileClose(file_handle);

int file_handle=FileOpen("out.txt", FILE_READ|FILE_TXT); means that you have your file "out.txt" in your folder, e.g. C:\Users\User1\AppData\Roaming\MetaQuotes\Terminal\999999DEA9630EA94D0715D755974F1D\MQL4\Files\out.txt. If you try in tester, the path is
C:\Users\User1\AppData\Roaming\MetaQuotes\Terminal\999999DEA9630EA94D0715D755974F1D\tester\files\out.txt Make sure you have the file there to solve the 5002 error.
It might happen that you successfully opened the file once but failed to close when wrote the code. and you cannot open it now. One way is to close MT4 (and it will close all open files), another way is to open files in SHARE mode.
int file_handle=FileOpen("out.txt", FILE_READ|FILE_SHARE_READ|FILE_TXT);

Related

Reading characters within a file (the fseek command) not working in nxc (not-exactly-c) programming

Basically I'm trying to write code that reads a specified value from a file ex: file contains(12345) the code reads specific place (say the 3rd place) and the output = 3
I already know how to write numbers to a file but I am stuck on how to read a character because the fseek command won't work (it doesn't know what the command is). I downloaded the include folder with all the extra commands but it still couldn't find the file "stdio.h" in the code.
I don't completely know how it all works anyway
I am a complete noob in programing and so I only know the very basic stuff. I am 90% sure it is my fault it isn't working.
#include "cstdio.h." //gets error (doesn't recognize command/file)
task main ()
{
byte handle;
int fsize = 100;
int in;
int sus = 12345;
DeleteFile("int.txt");
CreateFile("int.txt",100,handle);
Write(handle, sus);
CloseFile(handle);
OpenFileRead("int.txt",fsize,handle);
fseek(handle, 10, 3); //gets error (doesn't recognize command)
in = fgetc(handle);
ClearScreen();
NumOut(30,LCD_LINE5,in);
Wait(100000);
CloseFile(handle);
}

FileIsExist() not finding file

I am having an Expert Advisor (EA) look for the file "File.txt".
The file was created by a python program.
I can see the file in the file explorer.
The path to the file is
C:\Users\AppData\Roaming\MetaQuotes\Terminal\Common\Files.
The error code for the FileIsExist() function is 5020(ERR_FILE_NOT_EXIST).
Why does it not recognize the file? Is it looking in another directory?
while(!FileIsExist("File.txt", 0)){
if(FileIsExist("File.txt", 0))
printf("in while loop, waiting for file");
else{
int iErr = GetLastError();
printf(iErr);
}
}
If your file is in 'Common' folder, use the corresponding flag.
bool exist=FileIsExist(filename,FILE_COMMON);
What is the idea of your code? if file does not exsist - sleep for a while (10ms) then check again

Access Denied when creating file in Visual F#

The following code runs without a hitch:
On the other hand, I get an access-denied error with this:
The destination is in my personal folder and I have full control. The directory is not read-only. Anyway, in either of those cases, the first code sample should not run either! I appreciate the help ...
In the second sample, you have two problems:
There are back slashes instead of forward slashes, so some of them may get interpreted as escape sequences.
You completely ignore the first parameter of write and specify what I assume is a folder as destination. You can't open a file stream on a folder, no wonder you get access denied.
This should work:
let write filename (ms:MemoryStream) =
let path = System.IO.Path.Combine( "C:/Users/<whatever>/signal_processor", filename )
use fs = new FileStream( path, FileMode.Create )
ms.WriteTo(fs)

Illegal text-relocation error, how to embed a text file?

I am currently developing an application in which the first step its to load a text file and read its contents.
For example:
string file = "myfile.txt";
i am calling another function in which i want to look for a certain pattern/occurrence
void read(string pattern, const char *content) {
char subString [blocksize];
int n;
char *search = pattern;
pos = strstr(content,search);
.....
The function works perfectly on my Mac, but once I try to compile the application the compiler throws some error ld : illegal text-relocation.
The text file is in the same folder as the target app.
I'm assuming this has something to do with embedding the text file?
hope someone can help or give a hint !
thanks in advance

system.io.directorynotfound -> But it works in Console

My files are referenced like so (it's all relative):
// WHERE YOU KEEP THE PAGE TITLE XML
public static string myPageTitleXML = "xml/pagetitles.xml";
and
using (StreamReader r = new StreamReader(myPageTitleXML))
{ //etc.. . .etc....etc..
}
I get system.io.directorynotfound, and "this problem needs to be shut down", when I double click the executable. But running it from the console works like a charm. What's wrong here?
I played around with attempting to set Environment.CurrentDirectory but couldn't get anything to work. Why should I have to do that anyway? It defeats the purpose of a relative path no?
responding.. .
"application" does not exist in the current context, i'll keep trying what people have mentioned, this is not a windows.form
testing
Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML); gives error URI formats are not supported, as does Path.GetFullPath(). Server.MapPath results in an error as well, this is currently offline
Well assuming this directory is somewhere under the directory in which your code is executing, it sounds like you can use ..
Application.ExecutablePath()
or
Application.StartUpPath()
.. to get an idea as to what your application is seeing when it goes in search of an 'xml' directory with the 'pagetitles.xml' file in it.
If the directory returned by one of these methods does not point where you thought it did, you'll need to move the location of your application or the location of this folder so that it is within the same directory as the app.
Hope this gets you on the right path.
So, when you run it from double clicking the executable, is there a file named pagetitles.xml in a folder named xml, where xml is a folder in the same location as the executable?
It's certainly possible to use relative paths like this, but I wouldn't really recommend it. Instead, maybe use something like:
string fileToOpen = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), myPageTitleXML);
using (StreamReader r = new StreamReader(fileToOpen))
{
//etc.. . .etc....etc..
}
Is this ASP.NET code? If so then you probably need to do MapPath("xml/pagetitles.xml")

Resources