Scripting Resources for DigitalMicrograph™ |
Example: Reading Data from a Text File |
|
Function |
Example script to show how to read from a text file. The text file is simply a file created in a simple text editor or a script window, where each line is delimited by a carriage return. |
Version |
version:20100426, v1.0 |
Author |
D. R. G. Mitchell |
Acknowledgements |
|
Comments |
Requires a text file be provided (.txt) which can be created using NotePad or similar. Data should be one number per row delimited with a carriage return. |
System Requirements |
Should be compatible with all recent versions of DigitalMicrograph. |
Known Issues |
|
Supported |
Yes |
Included Files |
Main script file. |
Source Code |
// Example script to show how to read from a text file. The text file is simply a file // created in a simple text editor or a script window, where each line is delimited // by a carriage return. Create a simple text file with several lines of numbers. // Save the text file to the hard disk then open it with this script.
// version:20100426, v1.0 // D. R. G. Mitchell, v1.0, April 2010 // adminnospam@dmscripting.com (remove the nospam to make this work)
// It is necessary to open a file for reading (openfileforreading()) to read from it. // It must also be closed at the conclusion of reading, otherwise // it will not be released to other attempts to do anything with it. // The Try/Catch routine should ensure that it is closed after any // error. The readfileline() function will read the file line by line and return // 1 for every line it reads successfully. It will return 0 at the // end of the file and this is used to detect the end of file. For more information // search for 'File Input and Output' in DMs online help.
// This technique is useful to either create a set of tags or a settings file which // contain specific data. This is useful where quite large volumes of data need to be // accessible to the script. Typing it in by hand would be very tiresome and error prone. // It would also cause the script to become bloated and loading would be slow.
// For smaller volumes of data it may be more appropriate to create a self-contained // script which has the data hard coded into statements which create the tags ie the // script creates its own settings in the Global Info, rather than accessing data // either installed during installation or by an installer script.
// variables
string path number filereference string thisline
// Select the file
if(!opendialog(path)) exit(0)
// Try/Catch to open the file for reading but close it in case of error. try { filereference=openfileforreading(path) number readok=1
// While ever the end of file has not been reached (readok=0) read the next line // and output it to the results
while(readok==1) { readok=readfileline(filereference, thisline) result("\n"+thisline) } } catch // there was an error - close the file { closefile(filereference) showalert("Sorry there was an error",2) exit(0) }
// The file read has concluded, close the file for reading. closefile(filereference)
|