I do not understand how OpenFileAppend() in NXC should work.
My goal is to open file and write data to it. If a file with the specified name allready exist, I want to append to that file. Otherwise I want to create a new file.
Here is how I think to accomplish that:
Code: Select all
string FILE_NAME="test.csv";
byte file_handle;
unsigned int loader_result_code;
const int file_size=1024;
loader_result_code=CreateFile(FILE_NAME,file_size,file_handle);
if (loader_result_code == LDR_FILEEXISTS)
loader_result_code=OpenFileAppend(FILE_NAME,file_size,file_handle);
if (loader_result_code == LDR_SUCCESS)
loader_result_code=Write(file_handle,"succesfull");
What am I doing wrong?
I use Bricxcc 3.3.8.9 and NXT enhanced firmware 1.31 (2011-03-15)
I have created an other piece of code (down here) that illustrates the operation:
Code: Select all
string FILE_NAME="test.csv";
byte file_handle;
unsigned int loader_result_code;
const int file_size=1024;
task main()
{
loader_result_code=CreateFile(FILE_NAME,file_size,file_handle);
switch (loader_result_code)
{
case LDR_SUCCESS:
TextOut(0,LCD_LINE1,"created succesfull!");
loader_result_code=Write(file_handle,"succesfull");
break;
case LDR_FILEISFULL:
TextOut(0,LCD_LINE1,"file is full!");
break;
case LDR_FILEEXISTS:
TextOut(0,LCD_LINE1,"file exist.");
break;
case LDR_INPROGRESS:
TextOut(0,LCD_LINE1,"not yet completed.");
break;
case LDR_FILEISBUSY:
TextOut(0,LCD_LINE1,"file already used.");
break;
default:
TextOut(0,LCD_LINE1,"create failed!");
break;
}
TextOut(0,LCD_LINE2,"filesize="+NumToStr(file_size));
TextOut(0,LCD_LINE3,"result code="+FormatNum("%04x", loader_result_code));
TextOut(0,LCD_LINE4,"file_handle="+NumToStr(file_handle));
loader_result_code=OpenFileAppend(FILE_NAME,file_size,file_handle);
switch (loader_result_code)
{
case LDR_SUCCESS:
TextOut(0,LCD_LINE5,"append succesfull!");
loader_result_code=Write(file_handle,"succesfull");
break;
case LDR_FILEISFULL:
TextOut(0,LCD_LINE5,"file is full!");
break;
case LDR_INPROGRESS:
TextOut(0,LCD_LINE5,"not yet completed.");
break;
case LDR_FILEISBUSY:
TextOut(0,LCD_LINE5,"file already used.");
break;
default:
TextOut(0,LCD_LINE5,"append failed!");
break;
}
TextOut(0,LCD_LINE6,"filesize="+NumToStr(file_size));
TextOut(0,LCD_LINE7,"result code="+FormatNum("%04x", loader_result_code));
TextOut(0,LCD_LINE8,"file_handle="+NumToStr(file_handle));
Wait(80000);
CloseFile(FILE_NAME);
}
Code: Select all
created succesfu
filesize=1024
result code=0000
filehandle=1
file allready use
filesize=1024
result code=8b00
filehandle=2
The loader module error that were returned when the file was created (0000, LDR_SUCCESS) and when OpenFileAppend() was executed (8b00, LDR_FILEISBUSY) are as expected.
The next time I run the code this is the output on the NXT screen:
Code: Select all
file exist.
filesize=1024
result code=8f00
filehandle=1
file=full!
filesize=1024
result code=8e00
filehandle=1
Attempting to create the file will of course returns error 8f00, LDR_FILEEXISTS. When attempting to execute OpenFileAppend() the loader module returns error 8e00, LDR_FILEISFULL. That is not what I would expected. I would expected that it returns 0000, LDR_SUCCESS.
The link http://bricxcc.sourceforge.net/nbc/nxcd ... rrors.html gives an explanation of the loader module error codes, but they are not always clear to me.
I hope someone can help me out.
Best regards,
Ted Sluis