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);
}
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);
please give me some hint how to perform File handling in CAPL programming.
How to open and how to access the file , what and which types of functions are available for file handling.
give some suggestion regarding this topics.
Thank you.
CAPL syntax provides you with some functions. To open a file stream for reading, use openFileRead();.
You may use function fileGetString() to read lines from such file. A code snippet might look like
char input[30] = "test.txt"
int bufferLength = 256;
char buffer[bufferLength];
dword fh;
fh = openFileRead(input,0);
while(fileGetString(buffer,bufferLength,fh)!=0) {
// do whatever
}
Please put some more effort next time: this is not how StackOverflow works.
I'm new to lua, this might be something quite simple, but I couldn't figure it out. I did all night search, read some posts here, but are not quite what I'm looking for. I finally worked out a lame solution that i'm not happy with, so I'm here to ask for help.
I'm trying to embed lua inside c++, and this program will run as part of an app on the iPhone, as we know, every iPhone app has a resource bundle, and the lua scripts are distributed with the bundle.
// I printed out the bundle path:
bundle directory /var/mobile/Applications/C6CEF090-B99A-4B9B-ADAC-F0BEF46B6EA4/LuaThirdTry.app
Say I have two script files (main.lua, mylib.lua) in the same folder, I put them in my Xcode project, organized like this:
somefolder
|--main.lua
|--mylib.lua
and main.lua is as below:
--main.lua
print(package.path)
require("mylib")
obviously I want to use code from mylib.lua, however, I got error from lua vm:
/usr/local/share/lua/5.2/?.lua;/usr/local/share/lua/5.2/?/init.lua;/usr/local/lib/lua/5.2/?.lua;/usr/local/lib/lua/5.2/?/init.lua;./?.lua
PANIC: unprotected error in call to Lua API (...090-B99A-4B9B-ADAC-F0BEF46B6EA4/LuaThirdTry.app/test.lua:5: module 'mylib' not found:
no field package.preload['mylib']
no file '/usr/local/share/lua/5.2/mylib.lua'
no file '/usr/local/share/lua/5.2/mylib/init.lua'
no file '/usr/local/lib/lua/5.2/mylib.lua'
no file '/usr/local/lib/lua/5.2/mylib/init.lua'
no file './mylib.lua'
no file '/usr/local/lib/lua/5.2/mylib.so'
no file '/usr/local/lib/lua/5.2/loadall.so'
no file './mylib.so')
When I add a line modifying package.path, I got it running correctly:
--main.lua modified
print(package.path)
package.path = package.path .. ";/var/mobile/Applications/C6CEF090-B99A-4B9B-ADAC-F0BEF46B6EA4/LuaThirdTry.app/?.lua"
require("mylib")
But this is an absolute path, which should definitely be avoided.
One way to solve this problem is to provide lua a function from c, which returns the full path of lua file in ipa bundle at runtime to lua script, and concatenate the package.path with that path, but I think that shouldn't be the "official" way of doing this.
I noticed "./?.lua" inside package.path variable, I just wonder why mylib.lua can't be found, isn't it for files within the same directory?
Sorry for the blah, so the question is: how do I do the "require" decently in iOS environment?
Okay, I finally find a good answer and the job is done, thank to this answer.
So, the solution is to modify the path within c++ code, add this function
#include <string>
int setLuaPath(lua_State* L, const char* path) {
lua_getglobal(L, "package");
lua_getfield(L, -1, "path"); // get field "path" from table at top of stack (-1)
std::string cur_path = lua_tostring(L, -1); // grab path string from top of stack
cur_path.append(";"); // do your path magic here
cur_path.append(path);
lua_pop(L, 1); // get rid of the string on the stack we just pushed on line 5
lua_pushstring(L, cur_path.c_str()); // push the new one
lua_setfield(L, -2, "path"); // set the field "path" in table at -2 with value at top of stack
lua_pop(L, 1); // get rid of package table from top of stack
return 0; // all done!
}
and then, call this function when init lua_State:
// yourfile.cpp
void runLua() {
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
OCCaller oc_caller;
std::string bundlePath = oc_caller.get_ios_bundle_path();
bundlePath.append("/?.lua");
setLuaPath(L, bundlePath.c_str());
....
}
your oc_caller class might look like this:
// OCCaller.h
#ifndef __LuaThirdTry__OCCaller__
#define __LuaThirdTry__OCCaller__
#include <iostream>
class OCCaller {
public:
OCCaller();
~OCCaller();
std::string get_ios_bundle_path();
};
#endif
impl file:
// OCCaller.mm
#include "OCCaller.h"
#import <Foundation/Foundation.h>
OCCaller::OCCaller() { }
OCCaller::~OCCaller() { }
std::string OCCaller::get_ios_bundle_path() {
NSString *bundlePath = [[NSBundle mainBundle]resourcePath];
return std::string([bundlePath UTF8String]);
}
Most likely, "." is not the folder containing main.lua, but a working directory (such as where xcode runs from). The app that runs your script probably runs it via a path, like
lua /full/path/to/your/main.lua
So having ./?.lua in LUA_PATH does not help here. Instead, you should have the script run a command to determine where it is running from, and append that to package.path. This should be the path part of arg[0]. So you could try (not tested):
local scriptPath = arg[0]
local dir = string.match(scriptPath, '^.*/')
package.path = package.path .. ';' .. dir .. '?.lua'
The arg is automatically populated by the interpreter, see Section 6 of Lua ref man.
You definitely don't need C to do what you want.
This is a revision on Bryophyte's answer. To me, this was far too complex. I'm also not sure why he did all these extra classes and used C++ strings instead of NSStrings. Here is my revised and hopefully easier to understand iOS code based on this solution:
-(void)addBundlePathToLuaState:(lua_State*)L
{
lua_getglobal(L, "package");
lua_getfield(L, -1, "path"); // get field "path" from table at top of stack (-1)
const char* current_path_const = lua_tostring(L, -1); // grab path string from top of stack
NSString* current_path = [NSString stringWithFormat:#"%s;%#/?.lua", current_path_const, [[NSBundle mainBundle]resourcePath]];
lua_pop(L, 1); // get rid of the string on the stack we just pushed on line 5
lua_pushstring(L, [current_path UTF8String]); // push the new one
lua_setfield(L, -2, "path"); // set the field "path" in table at -2 with value at top of stack
lua_pop(L, 1); // get rid of package table from top of stack
}
On transferring a F# file -- Jira.fs to a script file -- Jira.fsx
I am encountering a problem with a constant string that uses triple quotes and within it contains double quotes (example below)
Jira.fs = success
Jira.fsx = fails with error FS0010: Unexpected symbol ':' in expression. Expected '}' or other token.
Warnings include - Warning: line too long, ignoring some characters
Is there a limit to .fsx scripts?
If so is there a recommended way around this? Just read in the string from a file?
Code below: thanks
[<Literal>]
let childIssueSchema = """ {"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"18043","self":"https://atlassian.au.MyCompany.net/jira/rest/api/latest/issue/18043","key":"DPMITPRODU-141","fields":{"progress":{"progress":0,"total":0},"summary":"APC - Calculator link on AOL ","customfield_10560":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/10340","value":"Yes","id":"10340"},"customfield_11067":null,"timetracking":{},"customfield_11066":null,"issuetype":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issuetype/6","id":"6","description":"A user story","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/sales.gif","name":"User Story","subtask":false},"customfield_10562":null,"customfield_11069":null,"customfield_11068":null,"customfield_11160":"4837","customfield_11161":null,"customfield_11660":null,"timespent":null,"reporter":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/user?username=lkaligotla","name":"lkaligotla","emailAddress":"lkaligotla#MyCompany.com.au","avatarUrls":{"16x16":"https://atlassian.au.MyCompany.net/jira/secure/useravatar?size=small&avatarId=10102","48x48":"https://atlassian.au.MyCompany.net/jira/secure/useravatar?avatarId=10102"},"displayName":"Lakshmi Kaligotla","active":true},"created":"2013-12-31T14:39:09.457+1100","updated":"2014-01-02T09:32:57.023+1100","customfield_10041":null,"priority":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/priority/3","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/priority_major.gif","name":"Medium","id":"3"},"description":"The current Age pension Calculator Icon must lead to a landing page with content and Start Calculator Button \r\n\r\nOn Click of start Calculator button calculator Home Page with instructions and start calculator button ","customfield_10002":null,"customfield_10003":null,"customfield_10040":null,"issuelinks":[{"id":"13177","self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issueLink/13177","type":{"id":"10010","name":"CrossProjectLink","inward":"part of","outward":"contains","self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issueLinkType/10010"},"inwardIssue":{"id":"18036","key":"DPMITPROJ-35","self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issue/18036","fields":{"summary":"APC - Calculator","status":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/status/10003","description":"User has placed this item in the queue","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/status_visible.gif","name":"In Queue","id":"10003"},"priority":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/priority/3","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/priority_major.gif","name":"Medium","id":"3"},"issuetype":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issuetype/5","id":"5","description":"A big user story that needs to be broken down.","iconUrl":"https://atlassian.au.MyCompany.net/jira/download/resources/com.pyxis.greenhopper.jira:greenhopper-webactions/images/ico_epic.png","name":"Epic","subtask":false}}}}],"customfield_10000":null,"customfield_10765":null,"subtasks":[],"customfield_10767":null,"status":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/status/10016","description":"","iconUrl":"https://atlassian.au.MyCompany.net/jira/images/icons/status_open.gif","name":"Backlog","id":"10016"},"labels":[],"workratio":-1,"project":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/project/DPMITPRODU","id":"11433","key":"DPMITPRODU","name":"DPMIT-Products","avatarUrls":{"16x16":"https://atlassian.au.MyCompany.net/jira/secure/projectavatar?size=small&pid=11433&avatarId=11680","48x48":"https://atlassian.au.MyCompany.net/jira/secure/projectavatar?pid=11433&avatarId=11680"}},"environment":null,"customfield_10053":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/10030","value":"Yes","id":"10030"},"aggregateprogress":{"progress":0,"total":0},"customfield_10050":"A link is available and on click leads to Calculor Landing page\r\nhttps://adviseronlineportal.com.au/Agepensioncalculator","components":[],"comment":{"startAt":0,"maxResults":0,"total":0,"comments":[]},"timeoriginalestimate":null,"customfield_10461":null,"customfield_10460":null,"customfield_11963":[{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/11441","value":"False","id":"11441"}],"customfield_10360":null,"votes":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issue/DPMITPRODU-141/votes","votes":0,"hasVoted":false},"customfield_10261":null,"customfield_10262":null,"customfield_10263":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/customFieldOption/10061","value":"Yes","id":"10061"},"fixVersions":[],"resolution":null,"resolutiondate":null,"aggregatetimeoriginalestimate":null,"customfield_10161":null,"customfield_10160":null,"duedate":null,"customfield_10020":null,"customfield_10060":"4793","watches":{"self":"https://atlassian.au.MyCompany.net/jira/rest/api/2/issue/DPMITPRODU-141/watchers","watchCount":1,"isWatching":false},"customfield_10162":null,"worklog":{"startAt":0,"maxResults":0,"total":0,"worklogs":[]},"assignee":null,"attachment":[],"aggregatetimeestimate":null,"versions":[],"timeestimate":null,"customfield_10030":null,"customfield_10031":null,"aggregatetimespent":null}} """
type ChildJsonSchema = FSharp.Data.JsonProvider< childIssueSchema >
I don't think there is a limit on the size of a script file (not sure what would happen around 2GB, but that does not sound like a realistic scenario), but for some reason (not sure why!) there is a limit on a single line length.
This is a bit unfortunate when you just want to copy & paste sample for a JSON type provider, but if you're using """ quotes, then you can just have a newline character in the string and it will still be treated as a single constant string:
[<Literal>]
let childIssueSchema = """{"foo":1,
"bar":42}"""
type ChildJsonSchema = FSharp.Data.JsonProvider<childIssueSchema>
That said, in case you have long and complex samples, it might be better to save them to files and just point the type provider to a file. Assuming you have childIssue.json in the same folder as the source file, you should be able to write:
type ChildJsonSchema = FSharp.Data.JsonProvider<"childIssue.json">