3.5.22 Scanning file

MathGL have possibilities to write textual information into file with variable values. In MGL script you can use save command for that. However, the usual printf(); is simple in C/C++ code. For example, lets create some textual file

FILE *fp=fopen("test.txt","w");
fprintf(fp,"This is test: 0 -> 1 q\n");
fprintf(fp,"This is test: 1 -> -1 q\n");
fprintf(fp,"This is test: 2 -> 0 q\n");
fclose(fp);

It contents look like

This is test: 0 -> 1 q
This is test: 1 -> -1 q
This is test: 2 -> 0 q

Let assume now that you want to read this values (i.e. [[0,1],[1,-1],[2,0]]) from the file. You can use scanfile for that. The desired values was written using template "This is test: %g -> %g q\n". So, just use

mglData a;
a.ScanFile("test.txt","This is test: %g -> %g");

and plot it to for assurance

gr->SetRanges(a.SubData(0), a.SubData(1));
gr->Axis();	gr->Plot(a.SubData(0),a.SubData(1),"o");

Note, I keep only the leading part of template (i.e. "This is test: %g -> %g" instead of "This is test: %g -> %g q\n"), because there is no important for us information after the second number in the line.