Below is a simple program highlighting the features of ROBOTC File IO:
/*****************************\
|* Example File Input/Output *|
|* Bence Feher 22/Dec/2011 *|
\*****************************/
task main()
{
TFileHandle hFileHandle; // will keep track of our file
TFileIOResult nIOResult; // will store our IO results
string sFileName = "test.txt"; // the name of our file
int nFileSize = 100; // will store our file size
char CR = 0x13; // define CR (carriage return)
char LF = 0x10; // define LF (line feed)
string sMessageToWrite = "ROBOTC IO test!"; // we will write this to the file
string sMesageToWrite_2 = "A new line!"; // we will also write this to the file on the next line
char incomingChar; // this will store each char as we read back in from the file
string incomingString[5]; // this will store the final, fully-read strings (with new strings getting put into new indexes
int nLineCounter = 0; // this will let us know which line we are on when reading and writing (used as the index to 'incommingString[]')
Delete("test.txt",nIOResult);
OpenWrite(hFileHandle, nIOResult, sFileName, nFileSize); // open the file for writing (creates the file if it does not exist)
WriteText(hFileHandle, nIOResult, sMessageToWrite); // write 'sMessageToWrite' to the file
WriteByte(hFileHandle, nIOResult, CR); // write 'CR' to the file (carriage return)
WriteByte(hFileHandle, nIOResult, LF); // write 'LF' to the file (line feed)
WriteText(hFileHandle, nIOResult, sMesageToWrite_2); // write 'sMesageToWrite_2' to the file
Close(hFileHandle, nIOResult); // close the file (DON'T FORGET THIS STEP!)
wait1Msec(1000); // just a wait so we can see the variables updating in the ROBOTC debugger in order (this is not necessary)
OpenRead(hFileHandle, nIOResult, sFileName, nFileSize); // open our file 'sFileName' for reading
for(int i = 0; i < nFileSize; i++) // iterate through the file until we've hit the end:
{
ReadByte(hFileHandle, nIOResult, incomingChar); // read in a single byte
if(incomingChar == CR || incomingChar == LF) // if the incomming byte is a carriage return or a line feed:
{
if(incomingChar == LF) // if it's a line feed:
nLineCounter++; // increment our index (will now store next char on a 'new line' (a new index into our 'incommingString[]')
}
else
{
//strncatSingleChar(pToBuffer, cSingleChar, nMaxBufferSize);
strncatSingleChar(incomingString[nLineCounter], incomingChar, sizeof(incomingString)); // append that byte (char) to the end of our final string, at the right index
}
}
Close(hFileHandle, nIOResult); // close our file (DON'T FORGET THIS STEP!)
for(int i = 1; i <= 5; i++)
{
nxtDisplayCenteredTextLine(i, incomingString[i-1]);
}
while(true)
{
wait1Msec(10);
} // infinite loop to keep our program running so that we can view the ROBOTC debugger variables
}
|
| void FindFirstFile(TFileHandle &hFileHandle, TFileIOResult &nIoResult, const string sSearch, string &sFileName, short &nFilesize)
|
(void) This function is used to start searching (iterating) through the list of files on the NXT. nIoResult is non-zero if no files exist in the NXT file system.. sSearch is the pattern match to use for the search string. For example:
- "*.*" will find all files
- "*.rso" will find all files with file extension “rso” which is the extension used for sound files
- sFileName is the name of the first file found and nFilesize is the number of data bytes in the file.
- hFileHandle returns a handle used to keep track of the search. You should use “Close” when the search is finished to release the handle so that the NXT can re-use it
|
| Parameter
|
Explanation
|
Data Type
|
| hFileHandle
|
The handle of the search.
|
TFileHandle
|
| nIoResult
|
The result of the search operation.
|
TFileIOResult
|
| sSearch
|
The pattern to search for.
|
string
|
| sFileName
|
The name of the first file found that matches the string pattern, 'sSearch'.
|
string
|
| nFilesize
|
The size of the file 'sFileName' in bytes, returned by the search operation.
|
short
|
|
TFileHandle myFileHandle; // create a file handle variable 'myFileHandle'
TFileIOResult IOResult; // create an IO result variable 'IOResult'
string myFileName = ""; // create and initialize a string variable 'myFileName'
int myFileSize = 0; // create and initialize an integer variable 'myFileSize'
FindFirstFile(myFileHandle, IOResult, "*.txt", myFileName, myFileSize); // return the first ".txt" file
|
|
| void FindNextFile(const TFileHandle hFileHandle, TFileIOResult &nIoResult, string &sFileName, short &nFilesize)
|
| (void) Finds the next file in a previously initiated search.
|
| Parameter
|
Explanation
|
Data Type
|
| hFileHandle
|
The handle of the search.
|
TFileHandle
|
| nIoResult
|
The result of the search operation.
|
TFileIOResult
|
| sFileName
|
The name of the first file found that matches the string pattern, 'sSearch'.
|
string
|
| nFilesize
|
The size of the file 'sFileName' in bytes, returned by the search operation.
|
short
|
|
TFileHandle myFileHandle; // create a file handle variable 'myFileHandle'
TFileIOResult IOResult; // create an IO result variable 'IOResult'
string myFileName = ""; // create and initialize a string variable 'myFileName'
int myFileSize = 0; // create and initialize an integer variable 'myFileSize'
FindFirstFile(myFileHandle, IOResult, "*.txt", myFileName, myFileSize); // store the name of the first txt file to 'myFileName'
string secondFile = "";
FindNextFile(myFileHandle, IOResult, secondFile, myFileSize); // store the name of the next txt file to 'secondFile'
|
|
| void OpenRead(TFileHandle &hFileHandle, TFileIOResult &nIoResult, const string &sFileName, word &nFileSize)
|
| (void) Opens sFileName for reading. hFileHandle is used for subsequent reads to this file. nFileSize is filled with the file length. nIoResult is non-zero when error occurs.
|
| Parameter
|
Explanation
|
Data Type
|
| hFileHandle
|
The handle assigned to the file once opened.
|
TFileHandle
|
| nIoResult
|
The result of the IO operation.
|
TFileIOResult
|
| sFileName
|
The name of the file to open.
|
string
|
| nFilesize
|
Will store the size (in bytes) of file opened.
|
word
|
|
TFileHandle myFileHandle; // create a file handle variable 'myFileHandle'
TFileIOResult IOResult; // create an IO result variable 'IOResult'
string myFileName = "myFile.txt"; // create and initialize a string variable 'myFileName'
int myFileSize = 0; // create and initialize an integer variable 'myFileSize'
OpenRead(myFileHandle, IOResult, myFileName, myFileSize); // open for read: "myFile.txt",
// storing its size in 'myFileSize'
|
|
| void OpenWrite(TFileHandle &hFileHandle, TFileIOResult &nIoResult, const string &sFileName, word &nFileSize)
|
| (void) Opens sFileName for writing with specified size of nFileSize. hFileHandle is used for subsequent writes to this file. nIoResult is non-zero when error occurs. sFileName must be a valid NXT file name. The file must not already exist on the NXT for this function to succeed.
|
| Parameter
|
Explanation
|
Data Type
|
| hFileHandle
|
The handle assigned to the file once opened.
|
TFileHandle
|
| nIoResult
|
The result of the IO operation.
|
TFileIOResult
|
| sFileName
|
The name of the file to open.
|
string
|
| nFilesize
|
Will store the size (in bytes) of file opened.
|
word
|
|
TFileHandle myFileHandle; // create a file handle variable 'myFileHandle'
TFileIOResult IOResult; // create an IO result variable 'IOResult'
string myFileName = "myFile.txt"; // create and initialize a string variable 'myFileName'
int myFileSize = 10; // create and initialize an integer variable 'myFileSize'
OpenWrite(myFileHandle, IOResult, myFileName, myFileSize); // open for write: "myFile.txt"
|
|