restoring the RedOrBlueCount data

In this section, you’ll change the RedOrBlueCount program so that the Total Red and Total Blue variables are initialized from the values saved in the RedBlueCounts file, instead of starting at zero. For each variable, you’ll use a File Access block to read the saved value from the file. If the file doesn’t already exist, the File Access block will fail, and the value from the Error data plug will be true. If this happens, you’ll just set the variable to zero.

The code that writes the values to the file writes Total Red first and then Total Blue. The code that reads the values from the file needs to use the same order, so you’ll initialize Total Red and then Total Blue. After reading the two values from the file, ...

Get The Art of LEGO MINDSTORMS NXT-G Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.