/* RESCUE ROBOTICS WORKSHOP ReadFromFile Introduction to File IO Ron Kessler Created 8/17/2018 Updated: 8/17/2018 Did final compile/test. Reads one byte of data from the NXT File (test.txt). PURPOSE: While the datalogger is good for real time sensor testing, you may need to save data in your own format. This project shows how to read a file on the robot and save a short integer (16 bits) to that file. Use the companion project called Save2File to store one piece of data. SETUP: Be sure to run the Save2File.c program before running this one. Your NXT screen will show you if the actions were sucessful or not and display your saved values. We will start by defining the file to read and the usual variables. */ task main() { //STEP 1: Define the stuff we need to access this file TFileHandle fileNumber; //internal number used to locate the file. TFileIOResult IOException; //tells us if there is an error during the IO string myFileName = "test.txt"; //my file name. int fileSize = 100; //same as when we saved 2 file //STEP 2: Tell the robot to open our file so we can read from it. Send it the 4 arguments in (). OpenRead(fileNumber, IOException, myFileName, fileSize); //---STEP 3: Create a variable to grab our data. It is a short integer (16bits or 2 bytes) short lineFromFile = 0; //---STEP 4: Now actually read the line of data and store it in the 'lineFromFile' variable. ReadShort(fileNumber, IOException, lineFromFile); //---STEP 5: ALWAYS CLOSE THE FILE Close(fileNumber, IOException); //---STEP 6: Display the data value on the NXT display. the %d or %i formats it as a number. nxtDisplayCenteredTextLine(1, "Rescue Robotics"); nxtDisplayCenteredTextLine(2, "Reading data"); nxtDisplayTextLine(5, "Your data = %d", lineFromFile); //---keep it on screen for 5 seconds so we can view it wait1Msec(5000); }