How to check how much file space is available? (NXC)
Posted: 17 Apr 2011, 21:07
I am working on a piece of code which creates a log file for various purposes. But if that log file already exists at the start of the program (created by a previous run of the program), the file must be re-opened to add data, using OpenFileAppend(FILE_NAME ,file_size,file_handle). My program will write strings to the log file using WriteLnString(file_handle ,string,bytes_written). If I keep on writing strings to the file it will reach at some point the end of the file and lose some data (that is, the part of the last string that was written to the file that didn't fit in).
(As known) NXC (or the NXT firmware) uses a fixed file size (that must be set when a file is created). The file can then be filled with data. If necessary, it is possible to increase the file size later.
I want to increase the file size well before it reaches end (LDR_FILEISFULL), using ResizeFile(FILE_NAME,new_file_size). Therefor it is very important to keep track of how many bytes are already written to the file and how much space is left in the file.
I have trouble keeping track of how many bytes are written to the file and how much free space there is within the file. Of course when I write data I can count the bytes. And I know the size of the file at creation time and how much I resized it every time it is almost full. But when I re-open the file after the program is restarted I don't know that. In that situation I don't know how many bytes have been written to the file (except when I read the the entire file and count the bytes). And when the NXT was turned off and on the file was cropped (all free space within the file was remove), but when a program was just stopped and started again the file isn't cropped.
My question is: Is there a simple way to determine the amount of bytes that are written to a file? Or a simple way how to determine the amount of free space available for writing in a file? Maybe with a single statement?
I hope someone can help me out.
Best regards,
Ted Sluis
(As known) NXC (or the NXT firmware) uses a fixed file size (that must be set when a file is created). The file can then be filled with data. If necessary, it is possible to increase the file size later.
I want to increase the file size well before it reaches end (LDR_FILEISFULL), using ResizeFile(FILE_NAME,new_file_size). Therefor it is very important to keep track of how many bytes are already written to the file and how much space is left in the file.
I have trouble keeping track of how many bytes are written to the file and how much free space there is within the file. Of course when I write data I can count the bytes. And I know the size of the file at creation time and how much I resized it every time it is almost full. But when I re-open the file after the program is restarted I don't know that. In that situation I don't know how many bytes have been written to the file (except when I read the the entire file and count the bytes). And when the NXT was turned off and on the file was cropped (all free space within the file was remove), but when a program was just stopped and started again the file isn't cropped.
My question is: Is there a simple way to determine the amount of bytes that are written to a file? Or a simple way how to determine the amount of free space available for writing in a file? Maybe with a single statement?
I hope someone can help me out.
Best regards,
Ted Sluis