How to check how much file space is available? (NXC)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
tedsluis
Posts: 3
Joined: 03 Apr 2011, 14:58
Location: Utrecht, The Netherlands
Contact:

How to check how much file space is available? (NXC)

Post by tedsluis »

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
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: How to check how much file space is available? (NXC)

Post by mattallen37 »

Can you have a second file that contains the information? Then when you restart the NXT and the program, it can know how much you have already written.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: How to check how much file space is available? (NXC)

Post by muntoo »

Use FreeMemory().

Code: Select all

task main()
{
    unsigned long x = FreeMemory();
    NumOut(0, 0, x, 0);
    Wait(2000);
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
sidneys1
Posts: 141
Joined: 01 Oct 2010, 14:38
Location: Pennsylvania - The United States of America
Contact:

Re: How to check how much file space is available? (NXC)

Post by sidneys1 »

muntoo wrote:Use FreeMemory().
If you'd read the first post, you'd know the questioner wants to know how much unused space there is.. in the file, not the filesystem. Perhaps you could read the file into memory, check the size of the variable, then subtract that from the file length... Is that possible in NXC?

Have a nice day,
~Sidneys1
My Mindstorms website: http://sidneys1.com
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: How to check how much file space is available? (NXC)

Post by muntoo »

sidneys1 wrote:If you'd read the first post, you'd know the questioner wants to know how much unused space there is.
Woops. :) And this was the first time I've done it...

In any case, the best practice* would be to include a header, regardless of whether NXC includes this feature or not (IMHO):

Code: Select all

struct FILE
{
    unsigned long size; // The amount of data written to the file so far. Not the actual file size.
    string data;
};
And then, as Sidneys1 suggests subtract the "true file size" to obtain the space left:

Code: Select all

FILE file;
OpenFileRead("log.txt", fsize, handle);
Read(handle, file); // not sure if strings will work...
Close(handle);

unsigned int fspace = fsize - (file.size + 4);

*In the real world, this ensures compatibility across different OSes/Filesystems/etc.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: How to check how much file space is available? (NXC)

Post by afanofosc »

With recent versions (1.2+) of the NXT firmware, the files that you open and write to are all truncated when your program exits so the next time you run your program there is no space left in the file. The call to OpenFileRead will tell you how many bytes are currently in the file via the fsize parameter. The file size returned by OpenFileAppend is the "available" space in the file but that will always be zero across two separate program executions. Within a single program execution if you close a file and reopen it with OpenFileAppend then the file size parameter should return a non-zero value indicating how many bytes are "left" in the file before it is full and needs resizing.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: How to check how much file space is available? (NXC)

Post by afanofosc »

The OP says the cropping occurs when you turn the NXT off and on but as written in the firmware source code the cropping should be occurring every time the cCmdDeactivateProgram function is called, which is any time you exit your program - either via "normal" exit or via the Abort key. It is called as part of the VM_RESET1 state. Pressing the abort key puts the VM into that state as does reaching the end of the program's executable code.

cCmdDeactivateProgram is also called when you power on the NXT so it would also do the cropping then. Here is the code from cCmdDeactivateProgram that performs the cropping function:

Code: Select all

  //Close any files we had opened programatically
  for (i = 0; i < MAX_HANDLES; i++)
  {
    //Copy i to tmp, because we pass a pointer to it to pFunc
    tmp = i;
    //Close file
    if (*(VarsCmd.FileHandleTable[i]) != 0)
      pMapLoader->pFunc(CROPDATAFILE, &tmp, NULL, NULL); /*CLOSE*/
  }
There was another recent thread on these forums about this cropping operation.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 0 guests