File I/O Commands

FileOpen(). FileOpen (drivenumber, filepath, filemode, fileID). Example: FileOpen ( 2, "System/myfile.txt", 1, uFileIDNum ).

drivenumber. The first item indicates the drive type. It is set to 2 for the removable memory card. This is the only supported media type at this time.

filepath. (String variable type s or t.) Sets the filename and path. If you want this file placed in a subdirectory, the folder must be present. MakeFolder() is used to create the folder.

filemode. Sets the file open mode: 1, Append; 2, Open at beginning of file, used for Read()/ReadLine(); 3, If the file already exists, the file is deleted and a new empty file is created. (If mode 3 is used to delete the previous file, you must close the file and reopen it in a different mode. Otherwise, the file will be deleted and recreated with every Write() or WriteLine().)

fileID. (Unsigned integer.) Stores the file ID number returned by FileOpen(). This value is used to reference the file in other file manipulation commands.

FileClose(). Closes the file specified by the ID uFileIDNum. Example: FileClose (uFileIDNum).

Write(). Writes data to the file identified by FileOpen(). It leaves the pointer at the end of the data written. Variables can be used for the data written, or a quoted string as in this example. Example: Write (uFileIDNum, "Some text").

WriteLine(). Same as Write(), but with a carriage return added to the end of the data written to the file. Example: WriteLine(uFileIDNum, "Some text").

SetDelimiter(). Required for Read function. End of line (the return character) always represents a delimiter regardless of the type of delimiter set. The delimiter is not returned with the data. Example: SetDelimiter ( delimitertype ). Delimiter Types: comma, comma delimiter; whitespace, all tabs and spaces between data/text, consecutive tabs and/or spaces are combined and considered one delimiter; tab, each individual tab is a delimiter; none, end of line (return character); no delimiter set, same as whitespace.

Read()/ReadLine(). Use mode 2 with FileOpen() for the read function. Read() leaves a pointer where the last delimiter was found. ReadLine() leaves the pointer at the beginning of the next line. FileClose() must be issued if you want to make sure the next read starts at the top of the file. As data is returned to each variable the pointer is moved to the next data item. In the following examples, the line of text needs to match the data type sequence i, u, f, b, s, and t. First line of the .txt file: 15, 287, 2.45, 1, string of text, more text data. If a comma delimiter is used, as in the following example, the items can be read individually: SetDelimiter (comma); Read( uFileIDNum, iVar, uVar, fVar, bVar, sVar, tVar ). If the data retrieved does not match the variable type used, the values will be affected. Generally, a null value is returned. Here, none is used as the delimiter, so it defaults to end of line: SetDelimiter ( none ); Read ( uFileIDNum, sVar1 ). This would return: "15,287,2.45,1, string of text, m" (s "string" variables have a 31 character limit). If a t variable were used in the above example, the full line would be returned, as it has a 255-character maximum.

— R.R.

Back to Article