FileIsExist() not finding file - mql4

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

Related

MQL4 error 5004 and 5002 when try to read file

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);

Lua check if a file is open

I use:
file = io.open(path)
to open a file specified by the path. Now, after some actions, I would use file:close() to close the file but there are some times that I didn't even open a file to close it after. How can I check whether a file has been open or not?
hjpotter92 suggestion works, the condition is not always false:
> if file then print("File was opened") else print("What File?") end
What File?
> file = io.open("file.txt")
> if file then print("File was opened") else print("What File?") end
File was opened
> file:close()
> file = nil -- assign to nil after closing the file.
> if file then print("File was opened") else print("What File?") end
What File?
>
if you follow this pattern then closing only an open file is easy:
if math.random() < .5 then
file = open("file.txt") -- maybe it opened
end
if file then -- close only if opened
file:close()
file = nil
end
A file is open if and only if it exists (not nil).
It is very hard using standard Lua calls to check if a file is open, however if it is just one script accessing the file You could set a variable to True when you open the file and check for it before closing it.
You may find this page helpful:
Lua check if a file is open or not

ILASM does not set FileVersion

I have an .il file which I can compile without any problems. I can strong name it and so without any issues. But I am not able to set the file version via the attribute as I would expect it. How can I set the FileVersion for an assembly when using ilasm?
If I do a round trip I get always a .res file which does contain only binary data which is not readable. What is inside this res file and can I edit it?
The code does not work
.assembly myAssembly
{
.custom instance void [mscorlib]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = { string('1.2.3.4') }
The issue can be solved by using the .res file. It is not sufficient to do a round trip with ildasm and ilasm. The IL file does not reference the .res file. I had to add it to the ilasm call manually. The data in the res file seemed to contain the infos which are written into the PE header which is ok for me.
The final command line needed was
ilasm test.il /dll /res:test.res
I still do not know what exactly is inside the res file but I can exhange it with the meta data information of any other assemlby that I create manually and then decompile it to replace the metadata of the original assembly as I need.
It seems not many people are doing such stuff.

how to set the path to where aapt add command adds the file

I'm using aapt tool to remove some files from different folders of my apk. This works fine.
But when I want to add files to the apk, the aapt tool add command doesn't let me specify the path to where I want the file to be added, therefore I can add files only to the root folder of the apk.
This is strange because I don't think that developers would never want to add files to a subfolder of the apk (res folder for example). Is this possible with aapt or any other method? Cause removing files from any folder works fine, and adding file works only for the root folder of the apk. Can't use it for any other folder.
Thanks
The aapt tool retains the directory structure specified in the add command, if you want to add something to an existing folder in an apk you simply must have a similar folder on your system and must specify each file to add fully listing the directory. Example
$ aapt list test.apk
res/drawable-hdpi/pic1.png
res/drawable-hdpi/pic2.png
AndroidManifest.xml
$ aapt remove test.apk res/drawable-hdpi/pic1.png
$ aapt add test.apk res/drawable-hdpi/pic1.png
The pic1.png that will is added resides in a folder in the current working directory of the terminal res/drawable-hdpi/ , hope this answered your question
There is actually a bug in aapt that will make this randomly impossible. The way it is supposed to work is as the other answer claims: paths are kept, unless you pass -k. Let's see how this is implemented:
The flag that controls whether the path is ignored is mJunkPath:
bool mJunkPath;
This variable is in a class called Bundle, and is controlled by two accessors:
bool getJunkPath(void) const { return mJunkPath; }
void setJunkPath(bool val) { mJunkPath = val; }
If the user specified -k at the command line, it is set to true:
case 'k':
bundle.setJunkPath(true);
break;
And, when the data is being added to the file, it is checked:
if (bundle->getJunkPath()) {
String8 storageName = String8(fileName).getPathLeaf();
printf(" '%s' as '%s'...\n", fileName, storageName.string());
result = zip->add(fileName, storageName.string(),
bundle->getCompressionMethod(), NULL);
} else {
printf(" '%s'...\n", fileName);
result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
}
Unfortunately, the one instance of Bundle used by the application is allocated in main on the stack, and there is no initialization of mJunkPath in the constructor, so the value of the variable is random; without a way to explicitly set it to false, on my system I (seemingly deterministically) am unable to add files at specified paths.
However, you can also just use zip, as an APK is simply a Zip file, and the zip tool works fine.
(For the record, I have not submitted the trivial fix for this as a patch to Android yet, if someone else wants to the world would likely be a better place. My experience with the Android code submission process was having to put up with an incredibly complex submission mechanism that in the end took six months for someone to get back to me, in some cases with minor modifications that could have just been made on their end were their submission process not so horribly complex. Given that there is a really easy workaround to this problem, I do not consider it important enough to bother with all of that again.)

How to read resource file from classpath in BlackBerry app?

I need to read a resource file from classpath in my BlackBerry application. The directory structure of my project is pretty common: under src directory there are 2 child dirs, one represents source packages root, another - resources root.
When I try to read any resource from classpath Class.getResourceAsStream method retures null
InputStream rStream = null;
String path = "/res/default_config.xml";
try {
rStream = getClass().getResourceAsStream(path);
} finally {
try {
if (rStream != null) {
byte[] data = IOUtilities.streamToBytes(rStream);
System.out.println(new String(data));
rStream.close();
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
How should I read classpath resource properly?
And have you tried to put xml file directly into src folder and use getClass().getResourceAsStream("default_config.xml"); ?
Actually cannot reproduce.
Tested on simulator 8800 eJDE 4.2.1.
File was placed in src/res/ folder.
I think you specified the path as incorrect way. You just remove the / from the beginning of the path you specified. If you are specifying /. then it will check for you resource folder
Even though it's generated as a COD file for running on the device, the JAR file is also created each build. It might be worth checking to make sure your xml file is being put in the directory that you expect it to be in as you can definitely store resources in sub-directories in your application and retrieve them using getClass().getResourceArStream();

Resources