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

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

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

Parsing LLVM IR code (with debug symbols) to map it back to the original source

I'm thinking about building a tool to help me visualise the generated LLVM-IR code for each instruction/function on my original source file.
Something like this but for LLVM-IR.
The steps to build such tool so far seem to be:
Start by with LLVM-IR AST builder.
Parse generated IR code.
On caret position get AST element.
Read the element scope, line, column and
file and signal it on the original source file.
Is this the correct way to approach it? Am I trivialising it too much?
I think your approach is quite correct. The UI part will probably be quite long to implement so I'll focus on the llvm part.
Let's say you start from a input file containing your LLVM-IR.
Step 1 process module:
Read file content to a string. Then Build a module from it, and process it to get the debug info:
llvm::MemoryBuffer* buf = llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(fileContent)).release();
llvm::SMDiagnostic diag;
llvm::Module* module = llvm::parseIR(buf->getMemBufferRef(), diag, *context).release();
llvm::DebugInfoFinder* dif = new llvm::DebugInfoFinder();
dif->processModule(*module);
Step 2 iterate on instructions:
Once done with that, you can simply iterate on function and blocks and instructions:
// pseudo code for loops (real code is a bit long)
foreach(llvm::Function f in module.functions)
{
foreach(llvm::BasicBlock b in f.BasicBlockList)
{
foreach(llvm::Instruction inst in b.InstList)
{
llvm::DebugLoc dl = inst.getDebugLoc();
unsigned line = dl->getLine();
// accordingly populate some dictionary between your instructions and source code
}
}
}
Step 3 update your UI
This is another story...

CopyBooks,.cpy files,WTX Design Studio

I am working on WTX design Studio with copybooks,i have a copybook but i dont have any corresponding sample input regarding that .cpy file .
Are there any means to generate a sample text file from copy book rather than writing the text document manually?
As bill said there are lots of ways depending on the tools available:
Ask for sample file
There are packages that can generate data. As I do not work at your site I do not know if any have been installed. They tend to be expensive though
Write a Cobol program (I presume the source is a mainframe) so it would have to be done on the mainframe.
I presume the source is a Mainframe, get on the Mainframe and use FileAid (or FileMaster or whatever they have) to setup the file. FileAid and its ilk let you edit files with a Cobol copybook.
Use the RecordEditor to create the file. You can import Cobol Copybooks into the RecordEditor and then use them to edit Cobol-Data files.
Use a RecordEditor macro to generate a file;
Write a Java / jython / JRuby program with Cobol interface package (have a look on sourceforge)
There are a lot of other possibilities, as I do not know what software or skills you have I can not really advise.
RecordEditor Macro to generate some numeric data:
/******************************************************************
* Purpose: RecordEditor Example Macroto generate numeric data for a file
*
* It is best to run this script from a Single Record Screen rather than
* a Table screen
*
*******************************************************************/
var rec = layout.getRecord(0)
var lines = RecordEditorData.view.createLines(20)
for (lineNo = 0; lineNo < 20; lineNo++) {
print(lineNo);
for (i=0; i < rec.getFieldCount(); i++) {
try {
lines[lineNo].getFieldValue(0, i).set(lineNo * 100 + i)
} catch(err) {
lines[lineNo].getFieldValue(0, i).set(i % 10)
}
}
}
RecordEditorData.view.addLines(-1,1, lines)
Output from the Macro:

Writing in binary files in Delphi

I'm making a program in delphi that writes data to a binary file and I have a problem I do not understand how to solve.
I have the following code:
testar: = TFileStream.Create ('test.exe', fmOpenWrite);
testar.Position: = testar.Size;
here: = '{test} test {test}';
testar.Write (here, 1024);
Tested with WinHex
http://img836.imageshack.us/img836/3206/la49.jpg
This edition fine prints in the binary code because when I see it with WinHex looks good, but this other code:
testar: = TFileStream.Create ('test.exe', fmOpenWrite);
testar.Position: = testar.Size;
here: = '{test}' + Edit1.Text + '{test}';
testar.Write (here, 1024);
It does not show anything at all because it seems that there is a problem with the edit when you want to edit the binary code, it's weird because when
I use it all goes single quotes but with the example of the edit does not work.
Note: The program does not give any error message
Someone could help me with this problem ?
You have provided non-real code, but I suspect that "here" is string.
To write string body to stream, you can use the next code:
test.Write(PChar(here)^, SizeOf(Char) * Length(here));
P.S. If you are going to read this string from stream (file) later, then it would be wise to write its length before string body.

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

Resources